writeback fix bdi thread race in mark_inode_dirty
PATCH 1/1 writeback fix bdi thread race in mark_inode_dirty
In __mark_inode_dirty, a process checks !wb_has_dirty_io outside of list_lock spinlock. This could cause a race, where process sees that list has dirty io and decides not wake up bdi thread and waits for spinlock to add to dirty list. Right at this time bdi_writeback_workfn finished write-back on last inode. It sees the list is empty and ends. Process could now get the spinlock and add inode to dirty list and doesn't wakeup bdi thread. Future calls to __mark_inode_dirty also do not wake up the thread because list is not empty any more.
Fix is to get wb.list_lock spinlock before checking the dirty list
--- fs/fs-writeback.c | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
fs/fs-writeback.c +3 -2
@@ -1173,6 +1173,9 @@ void __mark_inode_dirty(struct inode *inode, int flags)bool wakeup_bdi = false; bdi =inode_to_bdi (inode); +spin_unlock (&inode->i_lock); +spin_lock (&bdi->wb.list_lock); +if (bdi_cap_writeback_dirty (bdi)) {WARN (!test_bit (BDI_registered, &bdi->state),"bdi-%s not registered\n" , bdi->name);
@@ -1187,8 +1190,6 @@ void __mark_inode_dirty(struct inode *inode, int flags) wakeup_bdi = true; } -spin_unlock (&inode->i_lock); -spin_lock (&bdi->wb.list_lock); inode->dirtied_when = jiffies;list_move (&inode->i_wb_list, &bdi->wb.b_dirty);spin_unlock (&bdi->wb.list_lock);
Signature · 1 line
1.5.6.5
Quoted 9 lines · Srinivas Eeda
In
__mark_inode_dirty, a process checks !wb_has_dirty_io outside oflist_lockspinlock. This could cause a race, where process sees that list has dirty io and decides not wake up bdi thread and waits for spinlock to add to dirty list. Right at this timebdi_writeback_workfnfinished write-back on last inode. It sees the list is empty and ends. Process could now get the spinlock and add inode to dirty list and doesn't wakeup bdi thread. Future calls to__mark_inode_dirtyalso do not wake up the thread because list is not empty any more.
Quoted 3 lines
Fix is to get wb.list_lock spinlock before checking the dirty list
Signed-off-by Srinivas Eeda <[email protected]>
Good catch! The patch looks good. You can add:
Honza
Quoted 30 lines
--- fs/fs-writeback.c | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)fs/fs-writeback.c +3
-2@@ -1173,6 +1173,9 @@ void __mark_inode_dirty(struct inode *inode, int flags)bool wakeup_bdi = false; bdi =inode_to_bdi (inode); +spin_unlock (&inode->i_lock); +spin_lock (&bdi->wb.list_lock); +if (bdi_cap_writeback_dirty (bdi)) {WARN (!test_bit (BDI_registered, &bdi->state),"bdi-%s not registered\n" , bdi->name);@@ -1187,8 +1190,6 @@ void __mark_inode_dirty(struct inode *inode, int flags) wakeup_bdi = true; } -spin_unlock (&inode->i_lock); -spin_lock (&bdi->wb.list_lock); inode->dirtied_when = jiffies;list_move (&inode->i_wb_list, &bdi->wb.b_dirty);spin_unlock (&bdi->wb.list_lock);Signature · 1 line
1.5.6.5
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 10 lines · Srinivas Eeda
In
__mark_inode_dirty, a process checks !wb_has_dirty_io outside oflist_lockspinlock. This could cause a race, where process sees that list has dirty io and decides not wake up bdi thread and waits for spinlock to add to dirty list. Right at this timebdi_writeback_workfnfinished write-back on last inode. It sees the list is empty and ends. Process could now get the spinlock and add inode to dirty list and doesn't wakeup bdi thread. Future calls to__mark_inode_dirtyalso do not wake up the thread because list is not empty any more.Fix is to get wb.list_lock spinlock before checking the dirty list
With Jan's ack, lets add this for the current cycle. Should go into stable as well, imho.
Signature · 1 line
Jens Axboe
Hi Jens,
Thanks!
I am new to submitting patches to linux-fsdevel. Is there a mailing list I should submit this patch or someone will pick this patch for mainline and stable branch?
Thanks,
--Srini
Quoted 14 lines · Jens Axboe
Quoted 10 lines · Srinivas Eeda
In
__mark_inode_dirty, a process checks !wb_has_dirty_io outside oflist_lockspinlock. This could cause a race, where process sees that list has dirty io and decides not wake up bdi thread and waits for spinlock to add to dirty list. Right at this timebdi_writeback_workfnfinished write-back on last inode. It sees the list is empty and ends. Process could now get the spinlock and add inode to dirty list and doesn't wakeup bdi thread. Future calls to__mark_inode_dirtyalso do not wake up the thread because list is not empty any more.Fix is to get wb.list_lock spinlock before checking the dirty list
With Jan's ack, lets add this for the current cycle. Should go into stable as well, imho.
Quoted 7 lines · Srinivas Eeda
Hi Jens,
Thanks!
I am new to submitting patches to linux-fsdevel. Is there a mailing list I should submit this patch or someone will pick this patch for mainline and stable branch?
No that's fine. What is needed is a [email protected] CC, but that can easily be added after the fact.
Signature · 1 line
Jens Axboe