Skip to content

Commit 5eb6c7a

Browse files
Zach BrownLinus Torvalds
authored andcommitted
[PATCH] dio: lock refcount operations
The wait_for_more_bios() function name was poorly chosen. While looking to clean it up it I noticed that the dio struct refcounting between the bio completion and dio submission paths was racey. The bio submission path was simply freeing the dio struct if atomic_dec_and_test() indicated that it dropped the final reference. The aio bio completion path was dereferencing its dio struct pointer *after dropping its reference* based on the remaining number of references. These two paths could race and result in the aio bio completion path dereferencing a freed dio, though this was not observed in the wild. This moves the refcount under the bio lock so that bio completion can drop its reference and decide to wake all in one atomic step. Once testing and waking is locked dio_await_one() can test its sleeping condition and mark itself uninterruptible under the lock. It gets simpler and wait_for_more_bios() disappears. The addition of the interrupt masking spin lock acquiry in dio_bio_submit() looks alarming. This lock acquiry existed in that path before the recent dio completion patch set. We shouldn't expect significant performance regression from returning to the behaviour that existed before the completion clean up work. This passed 4k block ext3 O_DIRECT fsx and aio-stress on an SMP machine. Signed-off-by: Zach Brown <zach.brown@oracle.com> Cc: Badari Pulavarty <pbadari@us.ibm.com> Cc: Suparna Bhattacharya <suparna@in.ibm.com> Cc: 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 8459d86 commit 5eb6c7a

1 file changed

Lines changed: 45 additions & 31 deletions

File tree

‎fs/direct-io.c‎

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ struct dio {
121121
int page_errors; /* errno from get_user_pages() */
122122

123123
/* BIO completion state */
124-
atomic_t refcount; /* direct_io_worker() and bios */
125124
spinlock_t bio_lock; /* protects BIO fields below */
125+
unsigned long refcount; /* direct_io_worker() and bios */
126126
struct bio *bio_list; /* singly linked via bi_private */
127127
struct task_struct *waiter; /* waiting task (NULL if none) */
128128

@@ -267,19 +267,20 @@ static int dio_bio_complete(struct dio *dio, struct bio *bio);
267267
static int dio_bio_end_aio(struct bio *bio, unsigned int bytes_done, int error)
268268
{
269269
struct dio *dio = bio->bi_private;
270-
int waiter_holds_ref = 0;
271-
int remaining;
270+
unsigned long remaining;
271+
unsigned long flags;
272272

273273
if (bio->bi_size)
274274
return 1;
275275

276276
/* cleanup the bio */
277277
dio_bio_complete(dio, bio);
278278

279-
waiter_holds_ref = !!dio->waiter;
280-
remaining = atomic_sub_return(1, (&dio->refcount));
281-
if (remaining == 1 && waiter_holds_ref)
279+
spin_lock_irqsave(&dio->bio_lock, flags);
280+
remaining = --dio->refcount;
281+
if (remaining == 1 && dio->waiter)
282282
wake_up_process(dio->waiter);
283+
spin_unlock_irqrestore(&dio->bio_lock, flags);
283284

284285
if (remaining == 0) {
285286
int ret = dio_complete(dio, dio->iocb->ki_pos, 0);
@@ -308,7 +309,7 @@ static int dio_bio_end_io(struct bio *bio, unsigned int bytes_done, int error)
308309
spin_lock_irqsave(&dio->bio_lock, flags);
309310
bio->bi_private = dio->bio_list;
310311
dio->bio_list = bio;
311-
if ((atomic_sub_return(1, &dio->refcount) == 1) && dio->waiter)
312+
if (--dio->refcount == 1 && dio->waiter)
312313
wake_up_process(dio->waiter);
313314
spin_unlock_irqrestore(&dio->bio_lock, flags);
314315
return 0;
@@ -345,11 +346,17 @@ dio_bio_alloc(struct dio *dio, struct block_device *bdev,
345346
static void dio_bio_submit(struct dio *dio)
346347
{
347348
struct bio *bio = dio->bio;
349+
unsigned long flags;
348350

349351
bio->bi_private = dio;
350-
atomic_inc(&dio->refcount);
352+
353+
spin_lock_irqsave(&dio->bio_lock, flags);
354+
dio->refcount++;
355+
spin_unlock_irqrestore(&dio->bio_lock, flags);
356+
351357
if (dio->is_async && dio->rw == READ)
352358
bio_set_pages_dirty(bio);
359+
353360
submit_bio(dio->rw, bio);
354361

355362
dio->bio = NULL;
@@ -365,13 +372,6 @@ static void dio_cleanup(struct dio *dio)
365372
page_cache_release(dio_get_page(dio));
366373
}
367374

368-
static int wait_for_more_bios(struct dio *dio)
369-
{
370-
assert_spin_locked(&dio->bio_lock);
371-
372-
return (atomic_read(&dio->refcount) > 1) && (dio->bio_list == NULL);
373-
}
374-
375375
/*
376376
* Wait for the next BIO to complete. Remove it and return it. NULL is
377377
* returned once all BIOs have been completed. This must only be called once
@@ -384,16 +384,21 @@ static struct bio *dio_await_one(struct dio *dio)
384384
struct bio *bio = NULL;
385385

386386
spin_lock_irqsave(&dio->bio_lock, flags);
387-
while (wait_for_more_bios(dio)) {
388-
set_current_state(TASK_UNINTERRUPTIBLE);
389-
if (wait_for_more_bios(dio)) {
390-
dio->waiter = current;
391-
spin_unlock_irqrestore(&dio->bio_lock, flags);
392-
io_schedule();
393-
spin_lock_irqsave(&dio->bio_lock, flags);
394-
dio->waiter = NULL;
395-
}
396-
set_current_state(TASK_RUNNING);
387+
388+
/*
389+
* Wait as long as the list is empty and there are bios in flight. bio
390+
* completion drops the count, maybe adds to the list, and wakes while
391+
* holding the bio_lock so we don't need set_current_state()'s barrier
392+
* and can call it after testing our condition.
393+
*/
394+
while (dio->refcount > 1 && dio->bio_list == NULL) {
395+
__set_current_state(TASK_UNINTERRUPTIBLE);
396+
dio->waiter = current;
397+
spin_unlock_irqrestore(&dio->bio_lock, flags);
398+
io_schedule();
399+
/* wake up sets us TASK_RUNNING */
400+
spin_lock_irqsave(&dio->bio_lock, flags);
401+
dio->waiter = NULL;
397402
}
398403
if (dio->bio_list) {
399404
bio = dio->bio_list;
@@ -951,6 +956,7 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
951956
struct dio *dio)
952957
{
953958
unsigned long user_addr;
959+
unsigned long flags;
954960
int seg;
955961
ssize_t ret = 0;
956962
ssize_t ret2;
@@ -981,8 +987,8 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
981987
dio->iocb = iocb;
982988
dio->i_size = i_size_read(inode);
983989

984-
atomic_set(&dio->refcount, 1);
985990
spin_lock_init(&dio->bio_lock);
991+
dio->refcount = 1;
986992
dio->bio_list = NULL;
987993
dio->waiter = NULL;
988994

@@ -1092,12 +1098,20 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
10921098

10931099
/*
10941100
* 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().
1101+
* operation. AIO can if it was a broken operation described above or
1102+
* in fact if all the bios race to complete before we get here. In
1103+
* that case dio_complete() translates the EIOCBQUEUED into the proper
1104+
* return code that the caller will hand to aio_complete().
1105+
*
1106+
* This is managed by the bio_lock instead of being an atomic_t so that
1107+
* completion paths can drop their ref and use the remaining count to
1108+
* decide to wake the submission path atomically.
10991109
*/
1100-
if (atomic_dec_and_test(&dio->refcount)) {
1110+
spin_lock_irqsave(&dio->bio_lock, flags);
1111+
ret2 = --dio->refcount;
1112+
spin_unlock_irqrestore(&dio->bio_lock, flags);
1113+
BUG_ON(!dio->is_async && ret2 != 0);
1114+
if (ret2 == 0) {
11011115
ret = dio_complete(dio, offset, ret);
11021116
kfree(dio);
11031117
} else

0 commit comments

Comments
 (0)