Skip to content

Commit

Permalink
pin iocb through aio.
Browse files Browse the repository at this point in the history
commit b53119f upstream.

aio_poll() is not the only case that needs file pinned; worse, while
aio_read()/aio_write() can live without pinning iocb itself, the
proof is rather brittle and can easily break on later changes.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Cc: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
torvalds authored and gregkh committed May 2, 2019
1 parent d6b2615 commit c7f2525
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions fs/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,9 @@ static bool get_reqs_available(struct kioctx *ctx)
/* aio_get_req
* Allocate a slot for an aio request.
* Returns NULL if no requests are free.
*
* The refcount is initialized to 2 - one for the async op completion,
* one for the synchronous code that does this.
*/
static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
{
Expand All @@ -1028,7 +1031,7 @@ static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
percpu_ref_get(&ctx->reqs);
req->ki_ctx = ctx;
INIT_LIST_HEAD(&req->ki_list);
refcount_set(&req->ki_refcnt, 0);
refcount_set(&req->ki_refcnt, 2);
req->ki_eventfd = NULL;
return req;
}
Expand Down Expand Up @@ -1061,15 +1064,18 @@ static struct kioctx *lookup_ioctx(unsigned long ctx_id)
return ret;
}

static inline void iocb_destroy(struct aio_kiocb *iocb)
{
if (iocb->ki_filp)
fput(iocb->ki_filp);
percpu_ref_put(&iocb->ki_ctx->reqs);
kmem_cache_free(kiocb_cachep, iocb);
}

static inline void iocb_put(struct aio_kiocb *iocb)
{
if (refcount_read(&iocb->ki_refcnt) == 0 ||
refcount_dec_and_test(&iocb->ki_refcnt)) {
if (iocb->ki_filp)
fput(iocb->ki_filp);
percpu_ref_put(&iocb->ki_ctx->reqs);
kmem_cache_free(kiocb_cachep, iocb);
}
if (refcount_dec_and_test(&iocb->ki_refcnt))
iocb_destroy(iocb);
}

static void aio_fill_event(struct io_event *ev, struct aio_kiocb *iocb,
Expand Down Expand Up @@ -1743,9 +1749,6 @@ static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb)
INIT_LIST_HEAD(&req->wait.entry);
init_waitqueue_func_entry(&req->wait, aio_poll_wake);

/* one for removal from waitqueue, one for this function */
refcount_set(&aiocb->ki_refcnt, 2);

mask = vfs_poll(req->file, &apt.pt) & req->events;
if (unlikely(!req->head)) {
/* we did not manage to set up a waitqueue, done */
Expand Down Expand Up @@ -1776,7 +1779,6 @@ static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb)

if (mask)
aio_poll_complete(aiocb, mask);
iocb_put(aiocb);
return 0;
}

Expand Down Expand Up @@ -1867,18 +1869,21 @@ static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb,
break;
}

/* Done with the synchronous reference */
iocb_put(req);

/*
* If ret is 0, we'd either done aio_complete() ourselves or have
* arranged for that to be done asynchronously. Anything non-zero
* means that we need to destroy req ourselves.
*/
if (ret)
goto out_put_req;
return 0;
if (!ret)
return 0;

out_put_req:
if (req->ki_eventfd)
eventfd_ctx_put(req->ki_eventfd);
iocb_put(req);
iocb_destroy(req);
out_put_reqs_available:
put_reqs_available(ctx, 1);
return ret;
Expand Down

0 comments on commit c7f2525

Please sign in to comment.