aio: fix kioctx not being freed after cancellation at exit time

The recent changes overhauling fs/aio.c introduced a bug that results in
the kioctx not being freed when outstanding kiocbs are cancelled at
exit_aio() time.  Specifically, a kiocb that is cancelled has its
completion events discarded by batch_complete_aio(), which then fails to
wake up the process stuck in free_ioctx().  Fix this by modifying the
wait_event() condition in free_ioctx() appropriately.

This patch was tested with the cancel operation in the thread based code
posted yesterday.

[ fix build]
Signed-off-by: Benjamin LaHaise <>
Signed-off-by: Kent Overstreet <>
Cc: Kent Overstreet <>
Cc: Josh Boyer <>
Cc: Zach Brown <>
Signed-off-by: Andrew Morton <>
Signed-off-by: Linus Torvalds <>
diff --git a/fs/aio.c b/fs/aio.c
index 3fcdd73..7fe5bde 100644
--- a/fs/aio.c
+++ b/fs/aio.c
@@ -307,7 +307,9 @@
 	while (atomic_read(&ctx->reqs_active) > 0) {
-		wait_event(ctx->wait, head != ctx->tail);
+		wait_event(ctx->wait,
+				head != ctx->tail ||
+				atomic_read(&ctx->reqs_active) <= 0);
 		avail = (head <= ctx->tail ? ctx->tail : ctx->nr_events) - head;