Skip to content

Commit 8459d86

Browse files
Zach BrownLinus Torvalds
authored andcommitted
[PATCH] dio: only call aio_complete() after returning -EIOCBQUEUED
The only time it is safe to call aio_complete() is when the ->ki_retry function returns -EIOCBQUEUED to the AIO core. direct_io_worker() has historically done this by relying on its caller to translate positive return codes into -EIOCBQUEUED for the aio case. It did this by trying to keep conditionals in sync. direct_io_worker() knew when finished_one_bio() was going to call aio_complete(). It would reverse the test and wait and free the dio in the cases it thought that finished_one_bio() wasn't going to. Not surprisingly, it ended up getting it wrong. 'ret' could be a negative errno from the submission path but it failed to communicate this to finished_one_bio(). direct_io_worker() would return < 0, it's callers wouldn't raise -EIOCBQUEUED, and aio_complete() would be called. In the future finished_one_bio()'s tests wouldn't reflect this and aio_complete() would be called for a second time which can manifest as an oops. The previous cleanups have whittled the sync and async completion paths down to the point where we can collapse them and clearly reassert the invariant that we must only call aio_complete() after returning -EIOCBQUEUED. direct_io_worker() will only return -EIOCBQUEUED when it is not the last to drop the dio refcount and the aio bio completion path will only call aio_complete() when it is the last to drop the dio refcount. direct_io_worker() can ensure that it is the last to drop the reference count by waiting for bios to drain. It does this for sync ops, of course, and for partial dio writes that must fall back to buffered and for aio ops that saw errors during submission. This means that operations that end up waiting, even if they were issued as aio ops, will not call aio_complete() from dio. Instead we return the return code of the operation and let the aio core call aio_complete(). This is purposely done to fix a bug where AIO DIO file extensions would call aio_complete() before their callers have a chance to update i_size. Now that direct_io_worker() is explicitly returning -EIOCBQUEUED its callers no longer have to translate for it. XFS needs to be careful not to free resources that will be used during AIO completion if -EIOCBQUEUED is returned. We maintain the previous behaviour of trying to write fs metadata for O_SYNC aio+dio writes. Signed-off-by: Zach Brown <zach.brown@oracle.com> Cc: Badari Pulavarty <pbadari@us.ibm.com> Cc: Suparna Bhattacharya <suparna@in.ibm.com> Acked-by: Jeff Moyer <jmoyer@redhat.com> Cc: <xfs-masters@oss.sgi.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 20258b2 commit 8459d86

3 files changed

Lines changed: 39 additions & 62 deletions

File tree

‎fs/direct-io.c‎

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ static int dio_complete(struct dio *dio, loff_t offset, int ret)
226226
{
227227
ssize_t transferred = 0;
228228

229+
/*
230+
* AIO submission can race with bio completion to get here while
231+
* expecting to have the last io completed by bio completion.
232+
* In that case -EIOCBQUEUED is in fact not an error we want
233+
* to preserve through this call.
234+
*/
235+
if (ret == -EIOCBQUEUED)
236+
ret = 0;
237+
229238
if (dio->result) {
230239
transferred = dio->result;
231240

@@ -251,24 +260,6 @@ static int dio_complete(struct dio *dio, loff_t offset, int ret)
251260
return ret;
252261
}
253262

254-
/*
255-
* Called when a BIO has been processed. If the count goes to zero then IO is
256-
* complete and we can signal this to the AIO layer.
257-
*/
258-
static void dio_complete_aio(struct dio *dio)
259-
{
260-
int ret;
261-
262-
ret = dio_complete(dio, dio->iocb->ki_pos, 0);
263-
264-
/* Complete AIO later if falling back to buffered i/o */
265-
if (dio->result == dio->size ||
266-
((dio->rw == READ) && dio->result)) {
267-
aio_complete(dio->iocb, ret, 0);
268-
kfree(dio);
269-
}
270-
}
271-
272263
static int dio_bio_complete(struct dio *dio, struct bio *bio);
273264
/*
274265
* Asynchronous IO callback.
@@ -290,8 +281,11 @@ static int dio_bio_end_aio(struct bio *bio, unsigned int bytes_done, int error)
290281
if (remaining == 1 && waiter_holds_ref)
291282
wake_up_process(dio->waiter);
292283

293-
if (remaining == 0)
294-
dio_complete_aio(dio);
284+
if (remaining == 0) {
285+
int ret = dio_complete(dio, dio->iocb->ki_pos, 0);
286+
aio_complete(dio->iocb, ret, 0);
287+
kfree(dio);
288+
}
295289

296290
return 0;
297291
}
@@ -1082,47 +1076,33 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
10821076
mutex_unlock(&dio->inode->i_mutex);
10831077

10841078
/*
1085-
* OK, all BIOs are submitted, so we can decrement bio_count to truly
1086-
* reflect the number of to-be-processed BIOs.
1079+
* The only time we want to leave bios in flight is when a successful
1080+
* partial aio read or full aio write have been setup. In that case
1081+
* bio completion will call aio_complete. The only time it's safe to
1082+
* call aio_complete is when we return -EIOCBQUEUED, so we key on that.
1083+
* This had *better* be the only place that raises -EIOCBQUEUED.
10871084
*/
1088-
if (dio->is_async) {
1089-
int should_wait = 0;
1090-
1091-
if (dio->result < dio->size && (rw & WRITE)) {
1092-
dio->waiter = current;
1093-
should_wait = 1;
1094-
}
1095-
if (ret == 0)
1096-
ret = dio->result;
1097-
1098-
if (should_wait)
1099-
dio_await_completion(dio);
1100-
1101-
/* this can free the dio */
1102-
if (atomic_dec_and_test(&dio->refcount))
1103-
dio_complete_aio(dio);
1085+
BUG_ON(ret == -EIOCBQUEUED);
1086+
if (dio->is_async && ret == 0 && dio->result &&
1087+
((rw & READ) || (dio->result == dio->size)))
1088+
ret = -EIOCBQUEUED;
11041089

1105-
if (should_wait)
1106-
kfree(dio);
1107-
} else {
1090+
if (ret != -EIOCBQUEUED)
11081091
dio_await_completion(dio);
11091092

1093+
/*
1094+
* Sync will always be dropping the final ref and completing the
1095+
* operation. AIO can if it was a broken operation described above
1096+
* or in fact if all the bios race to complete before we get here.
1097+
* In that case dio_complete() translates the EIOCBQUEUED into
1098+
* the proper return code that the caller will hand to aio_complete().
1099+
*/
1100+
if (atomic_dec_and_test(&dio->refcount)) {
11101101
ret = dio_complete(dio, offset, ret);
1102+
kfree(dio);
1103+
} else
1104+
BUG_ON(ret != -EIOCBQUEUED);
11111105

1112-
/* We could have also come here on an AIO file extend */
1113-
if (!is_sync_kiocb(iocb) && (rw & WRITE) &&
1114-
ret >= 0 && dio->result == dio->size)
1115-
/*
1116-
* For AIO writes where we have completed the
1117-
* i/o, we have to mark the the aio complete.
1118-
*/
1119-
aio_complete(iocb, ret, 0);
1120-
1121-
if (atomic_dec_and_test(&dio->refcount))
1122-
kfree(dio);
1123-
else
1124-
BUG();
1125-
}
11261106
return ret;
11271107
}
11281108

‎fs/xfs/linux-2.6/xfs_aops.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ xfs_vm_direct_IO(
14061406
xfs_end_io_direct);
14071407
}
14081408

1409-
if (unlikely(ret <= 0 && iocb->private))
1409+
if (unlikely(ret != -EIOCBQUEUED && iocb->private))
14101410
xfs_destroy_ioend(iocb->private);
14111411
return ret;
14121412
}

‎mm/filemap.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,8 +1181,6 @@ generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
11811181
if (pos < size) {
11821182
retval = generic_file_direct_IO(READ, iocb,
11831183
iov, pos, nr_segs);
1184-
if (retval > 0 && !is_sync_kiocb(iocb))
1185-
retval = -EIOCBQUEUED;
11861184
if (retval > 0)
11871185
*ppos = pos + retval;
11881186
}
@@ -2047,15 +2045,14 @@ generic_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
20472045
* Sync the fs metadata but not the minor inode changes and
20482046
* of course not the data as we did direct DMA for the IO.
20492047
* i_mutex is held, which protects generic_osync_inode() from
2050-
* livelocking.
2048+
* livelocking. AIO O_DIRECT ops attempt to sync metadata here.
20512049
*/
2052-
if (written >= 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2050+
if ((written >= 0 || written == -EIOCBQUEUED) &&
2051+
((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
20532052
int err = generic_osync_inode(inode, mapping, OSYNC_METADATA);
20542053
if (err < 0)
20552054
written = err;
20562055
}
2057-
if (written == count && !is_sync_kiocb(iocb))
2058-
written = -EIOCBQUEUED;
20592056
return written;
20602057
}
20612058
EXPORT_SYMBOL(generic_file_direct_write);

0 commit comments

Comments
 (0)