gfs2: Handle page faults during read and write
Subsystems (6)
Andreas Gruenbacher
maintainercover gfs2: Handle page faults during read and write
Hello,
this is an update to my previous posting [1] on how to deal with potential mmap + page fault deadlocks in gfs2.
What's happening is that a page fault triggers during a read or write operation, while we're holding a glock (the cluster-wide gfs2 inode lock), and the page fault requires another glock. We can recognize and handle the case when both glocks are the same, but when the page fault requires another glock, there is a chance that taking that other glock will deadlock.
The solution I've come up with for this is to turn locking requests into locking attempts when we're in a potential recursive locking situation, and to actively fault in pages and retry at the outer level when a locking attempt fails. Those kinds of conflicts should be relatively rare.
Note that we need to fault in pages writable in ->read_iter, so this patch set adds a new iov_iter_fault_in_writeable() helper hat mirrors iov_iter_fault_in_readable().
In the initial prototype [1], I've added a restart_hack flag to struct task_struct; this has now been moved to the lower two bits of current->journal_info.
I've posted a new fstest [2] that triggers the self-recursion case so that those kind of bugs will be caught early next time, with no feedback in the last two weeks.
Those patches are currently on the gfs2 for-next branch [3]. If there are no objections, I'll ask Linus to pull them from there.
Thanks,
Andreas
[1] [RFC] Trigger retry from fault vm operation,
https://lore.kernel.org/linux-fsdevel/20210511140113.1225981-1-agruenba at redhat.com/
[2] https://lore.kernel.org/fstests/20210520114218.1595735-1-agruenba at redhat.com/T/#u
[3] https://git.kernel.org/pub/scm/linux/kernel/git/gfs2/linux-gfs2.git/log/?h=for-next
Andreas Gruenbacher (6):
gfs2: Fix mmap + page fault deadlocks (part 1)
iov_iter: Add iov_iter_fault_in_writeable()
gfs2: Add wrappers for accessing journal_info
gfs2: Encode glock holding and retry flags in journal_info
gfs2: Add LM_FLAG_OUTER glock holder flag
gfs2: Fix mmap + page fault deadlocks (part 2)
fs/gfs2/aops.c | 6 +-- fs/gfs2/bmap.c | 31 +++++++------- fs/gfs2/file.c | 102 +++++++++++++++++++++++++++++++++++++------- fs/gfs2/glock.c | 12 ++++++ fs/gfs2/glock.h | 13 ++++-- fs/gfs2/incore.h | 41 ++++++++++++++++++ fs/gfs2/inode.c | 2 +- fs/gfs2/log.c | 4 +- fs/gfs2/lops.c | 2 +- fs/gfs2/meta_io.c | 6 +-- fs/gfs2/super.c | 2 +- fs/gfs2/trans.c | 16 +++---- include/linux/uio.h | 1 + lib/iov_iter.c | 20 ++++++++- 14 files changed, 204 insertions(+), 54 deletions(-)
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH 1/6 gfs2: Fix mmap + page fault deadlocks (part 1)
When the buffer passed to a read or write system call is memory mapped to the same file, a page fault can occur in gfs2_fault. In that case, the task will already be holding the inode glock, and trying to take it again will result in a BUG in add_to_queue(). Fix that by recognizing the self-recursion case and either skipping the lock taking (when the glock is held in a compatible way), or fail the operation.
Likewise, a request to un-share a copy-on-write page can *probably* happen in similar situations, so treat the locking in gfs2_page_mkwrite in the same way.
A future patch will handle this case more gracefully, along with addressing more complex deadlock scenarios.
--- fs/gfs2/file.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-)
fs/gfs2/file.c +30 -10
@@ -423,6 +423,7 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)struct page *page = vmf->page;struct inode *inode =file_inode (vmf->vma->vm_file);struct gfs2_inode *ip =GFS2_I (inode); +struct gfs2_holder *outer_gh =gfs2_glock_is_locked_by_me (ip->i_gl);struct gfs2_sbd *sdp =GFS2_SB (inode);struct gfs2_alloc_parms ap = { .aflags =0 , };u64 offset =page_offset (page);
@@ -436,10 +437,18 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)sb_start_pagefault (inode->i_sb);gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE ,0 , &gh); - err =gfs2_glock_nq (&gh); -if (err) { - ret =block_page_mkwrite_return (err); - goto out_uninit; +if (likely (!outer_gh)) { + err =gfs2_glock_nq (&gh); +if (err) { + ret =block_page_mkwrite_return (err); + goto out_uninit; + } + }else { +if (!gfs2_holder_is_compatible (outer_gh,LM_ST_EXCLUSIVE )) { +/* We could try to upgrade outer_gh here. */ + ret =VM_FAULT_SIGBUS ; + goto out_uninit; + } }/* Check page index against inode size */
@@ -540,7 +549,8 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf) out_quota_unlock:gfs2_quota_unlock (ip); out_unlock: -gfs2_glock_dq (&gh); +if (likely (!outer_gh)) +gfs2_glock_dq (&gh); out_uninit:gfs2_holder_uninit (&gh);if (ret ==VM_FAULT_LOCKED ) {
@@ -555,6 +565,7 @@ static vm_fault_t gfs2_fault(struct vm_fault *vmf) {struct inode *inode =file_inode (vmf->vma->vm_file);struct gfs2_inode *ip =GFS2_I (inode); +struct gfs2_holder *outer_gh =gfs2_glock_is_locked_by_me (ip->i_gl);struct gfs2_holder gh;vm_fault_t ret;u16 state;
@@ -562,13 +573,22 @@ static vm_fault_t gfs2_fault(struct vm_fault *vmf) state = (vmf->flags &FAULT_FLAG_WRITE ) ?LM_ST_EXCLUSIVE :LM_ST_SHARED ;gfs2_holder_init (ip->i_gl, state,0 , &gh); - err =gfs2_glock_nq (&gh); -if (err) { - ret =block_page_mkwrite_return (err); - goto out_uninit; +if (likely (!outer_gh)) { + err =gfs2_glock_nq (&gh); +if (err) { + ret =block_page_mkwrite_return (err); + goto out_uninit; + } + }else { +if (!gfs2_holder_is_compatible (outer_gh, state)) { +/* We could try to upgrade outer_gh here. */ + ret =VM_FAULT_SIGBUS ; + goto out_uninit; + } } ret =filemap_fault (vmf); -gfs2_glock_dq (&gh); +if (likely (!outer_gh)) +gfs2_glock_dq (&gh); out_uninit:gfs2_holder_uninit (&gh);return ret;
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH 2/6 iov_iter: Add iov_iter_fault_in_writeable()
In reply to Andreas GruenbacherAdd the equivalent of iov_iter_fault_in_readable(), but for pages that will be written to.
While at it, fix an indentation error in iov_iter_fault_in_readable().
--- include/linux/uio.h | 1 + lib/iov_iter.c | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-)
include/linux/uio.h +1 -0
@@ -124,6 +124,7 @@ size_t iov_iter_copy_from_user_atomic(struct page *page,void iov_iter_advance (struct iov_iter *i,size_t bytes);void iov_iter_revert (struct iov_iter *i,size_t bytes);int iov_iter_fault_in_readable (struct iov_iter *i,size_t bytes); +int iov_iter_fault_in_writeable (struct iov_iter *i,size_t bytes);size_t iov_iter_single_seg_count (const struct iov_iter *i);size_t copy_page_to_iter (struct page *page,size_t offset,size_t bytes,struct iov_iter *i);
lib/iov_iter.c +19 -1
@@ -480,13 +480,31 @@ int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes)iterate_iovec (i, bytes, v, iov, skip, ({ err =fault_in_pages_readable (v.iov_base, v.iov_len);if (unlikely (err)) -return err; +return err; 0;})) }return 0 ; }EXPORT_SYMBOL (iov_iter_fault_in_readable); +int iov_iter_fault_in_writeable (struct iov_iter *i,size_t bytes) +{ +size_t skip = i->iov_offset; +const struct iovec *iov; +int err; +struct iovec v; + +if (!(i->type & (ITER_BVEC |ITER_KVEC ))) { +iterate_iovec (i, bytes, v, iov, skip, ({ + err =fault_in_pages_writeable (v.iov_base, v.iov_len); +if (unlikely (err)) +return err; + 0;})) + } +return 0 ; +} +EXPORT_SYMBOL (iov_iter_fault_in_writeable); +void iov_iter_init (struct iov_iter *i,unsigned int direction,const struct iovec *iov,unsigned long nr_segs,size_t count)
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH 3/6 gfs2: Add wrappers for accessing journal_info
In reply to Andreas GruenbacherNo longer access current->journal_info directly. We'll use that to encode additional information in current->journal_info later.
--- fs/gfs2/aops.c | 6 +++--- fs/gfs2/bmap.c | 28 ++++++++++++++-------------- fs/gfs2/incore.h | 10 ++++++++++ fs/gfs2/inode.c | 2 +- fs/gfs2/log.c | 4 ++-- fs/gfs2/lops.c | 2 +- fs/gfs2/meta_io.c | 6 +++--- fs/gfs2/super.c | 2 +- fs/gfs2/trans.c | 16 ++++++++-------- 9 files changed, 43 insertions(+), 33 deletions(-)
fs/gfs2/aops.c +3 -3
@@ -95,7 +95,7 @@ static int gfs2_writepage(struct page *page, struct writeback_control *wbc)if (gfs2_assert_withdraw (sdp,gfs2_glock_is_held_excl (ip->i_gl))) goto out; -if (current->journal_info) +if (current_trans ()) goto redirty;return iomap_writepage (page, wbc, &wpc, &gfs2_writeback_ops);
@@ -182,7 +182,7 @@ static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbcif (gfs2_assert_withdraw (sdp,gfs2_glock_is_held_excl (ip->i_gl))) goto out; -if (PageChecked (page) || current->journal_info) +if (PageChecked (page) ||current_trans ()) goto out_ignore;return __gfs2_jdata_writepage (page, wbc);
@@ -620,7 +620,7 @@ void adjust_fs_space(struct inode *inode)static int jdata_set_page_dirty (struct page *page) { -if (current->journal_info) +if (current_trans ())SetPageChecked (page);return __set_page_dirty_buffers (page); }
fs/gfs2/bmap.c +14 -14
@@ -1016,7 +1016,7 @@ static void gfs2_iomap_page_done(struct inode *inode, loff_t pos, unsigned copied, struct page *page, struct iomap *iomap) { - struct gfs2_trans *tr = current->journal_info; + struct gfs2_trans *tr = current_trans();struct gfs2_inode *ip =GFS2_I (inode);struct gfs2_sbd *sdp =GFS2_SB (inode);
@@ -1099,7 +1099,7 @@ static int gfs2_iomap_begin_write(struct inode *inode, loff_t pos, } } - tr = current->journal_info; + tr =current_trans ();if (tr->tr_num_buf_new)__mark_inode_dirty (inode,I_DIRTY_DATASYNC );
@@ -1347,7 +1347,7 @@ int gfs2_alloc_extent(struct inode *inode, u64 lblock, u64 *dblock,static int gfs2_block_zero_range (struct inode *inode,loff_t from,unsigned int length) { -BUG_ON (current->journal_info); +BUG_ON (current_trans ());return iomap_zero_range (inode, from, length,NULL , &gfs2_iomap_ops); }
@@ -1386,7 +1386,7 @@ static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsizetruncate_pagecache (inode, oldsize - chunk); oldsize -= chunk; - tr = current->journal_info; + tr =current_trans ();if (!test_bit (TR_TOUCHED , &tr->tr_flags))continue ;
@@ -1447,7 +1447,7 @@ static int trunc_start(struct inode *inode, u64 newsize) out:brelse (dibh); -if (current->journal_info) +if (current_trans ())gfs2_trans_end (sdp);return error; }
@@ -1555,7 +1555,7 @@ static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh,the rgrp. So we estimate. We know it can't be more than the dinode's i_blocks and we don't want to exceed the journal flush threshold, sd_log_thresh2. */ -if (current->journal_info ==NULL ) { +if (!current_trans ()) {unsigned int jblocks_rqsted, revokes; jblocks_rqsted = rgd->rd_length + RES_DINODE +
@@ -1577,7 +1577,7 @@ static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh,down_write (&ip->i_rw_mutex); }/* check if we will exceed the transaction blocks requested */ - tr = current->journal_info; + tr =current_trans ();if (tr->tr_num_buf_new +RES_STATFS +RES_QUOTA >=atomic_read (&sdp->sd_log_thresh2)) {/* We set blks_outside_rgrp to ensure the loop will
@@ -1625,7 +1625,7 @@ static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh,if (!ret && blks_outside_rgrp) {/* If buffer still has non-zero blocks outside the rgrp we just processed, do it all over again. */ -if (current->journal_info) { +if (current_trans ()) {struct buffer_head *dibh; ret =gfs2_meta_inode_buffer (ip, &dibh);
@@ -1991,7 +1991,7 @@ static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length) }if (btotal) { -if (current->journal_info ==NULL ) { +if (!current_trans ()) { ret =gfs2_trans_begin (sdp,RES_DINODE +RES_STATFS +RES_QUOTA ,0 );if (ret)
@@ -2011,7 +2011,7 @@ static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length) out:if (gfs2_holder_initialized (&rd_gh))gfs2_glock_dq_uninit (&rd_gh); -if (current->journal_info) { +if (current_trans ()) {up_write (&ip->i_rw_mutex);gfs2_trans_end (sdp);cond_resched ();
@@ -2436,7 +2436,7 @@ static int gfs2_journaled_truncate_range(struct inode *inode, loff_t offset, offset += chunk; length -= chunk; - tr = current->journal_info; + tr =current_trans ();if (!test_bit (TR_TOUCHED , &tr->tr_flags))continue ;
@@ -2501,7 +2501,7 @@ int __gfs2_punch_hole(struct file *file, loff_t offset, loff_t length) }if (gfs2_is_jdata (ip)) { -BUG_ON (!current->journal_info); +BUG_ON (!current_trans ());gfs2_journaled_truncate_range (inode, offset, length); }else truncate_pagecache_range (inode, offset, offset + length -1 );
@@ -2509,14 +2509,14 @@ int __gfs2_punch_hole(struct file *file, loff_t offset, loff_t length)file_update_time (file);mark_inode_dirty (inode); -if (current->journal_info) +if (current_trans ())gfs2_trans_end (sdp);if (!gfs2_is_stuffed (ip)) error =punch_hole (ip, offset, length); out: -if (current->journal_info) +if (current_trans ())gfs2_trans_end (sdp);return error; }
fs/gfs2/incore.h +10 -0
@@ -871,5 +871,15 @@ static inline unsigned gfs2_max_stuffed_size(const struct gfs2_inode *ip) return GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize - sizeof(struct gfs2_dinode); } +static inline struct gfs2_trans *current_trans (void ) +{ +return current->journal_info; +} + +static inline void set_current_trans (struct gfs2_trans *tr) +{ + current->journal_info = tr; +} +#endif /* __INCORE_DOT_H__ */
fs/gfs2/inode.c +1 -1
@@ -1883,7 +1883,7 @@ static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr) {int error; -if (current->journal_info) +if (current_trans ())return __gfs2_setattr_simple (inode, attr); error =gfs2_trans_begin (GFS2_SB (inode),RES_DINODE ,0 );
fs/gfs2/log.c +2 -2
@@ -204,7 +204,7 @@ void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc) ret =0 ;if (time_after (jiffies, flush_start + (HZ *600 ))) {fs_err (sdp,"Error: In %s for ten minutes! t=%d\n" , - __func__, current->journal_info ?1 :0 ); + __func__,current_trans () ?1 :0 );dump_ail_list (sdp); goto out; }
@@ -971,7 +971,7 @@ static void empty_ail1_list(struct gfs2_sbd *sdp)for (;;) {if (time_after (jiffies, start + (HZ *600 ))) {fs_err (sdp,"Error: In %s for 10 minutes! t=%d\n" , - __func__, current->journal_info ?1 :0 ); + __func__,current_trans () ?1 :0 );dump_ail_list (sdp);return ; }
fs/gfs2/lops.c +1 -1
@@ -43,7 +43,7 @@ void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh) {struct gfs2_bufdata *bd; -BUG_ON (!current->journal_info); +BUG_ON (!current_trans ());clear_buffer_dirty (bh);if (test_set_buffer_pinned (bh))
fs/gfs2/meta_io.c +3 -3
@@ -294,7 +294,7 @@ int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags, bh = *bhp;wait_on_buffer (bh);if (unlikely (!buffer_uptodate (bh))) { -struct gfs2_trans *tr = current->journal_info; +struct gfs2_trans *tr =current_trans ();if (tr &&test_bit (TR_TOUCHED , &tr->tr_flags))gfs2_io_error_bh_wd (sdp, bh);brelse (bh);
@@ -321,7 +321,7 @@ int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)wait_on_buffer (bh);if (!buffer_uptodate (bh)) { -struct gfs2_trans *tr = current->journal_info; +struct gfs2_trans *tr =current_trans ();if (tr &&test_bit (TR_TOUCHED , &tr->tr_flags))gfs2_io_error_bh_wd (sdp, bh);return -EIO ;
@@ -337,7 +337,7 @@ void gfs2_remove_from_journal(struct buffer_head *bh, int meta)struct address_space *mapping = bh->b_page->mapping;struct gfs2_sbd *sdp =gfs2_mapping2sbd (mapping);struct gfs2_bufdata *bd = bh->b_private; -struct gfs2_trans *tr = current->journal_info; +struct gfs2_trans *tr =current_trans ();int was_pinned =0 ;if (test_clear_buffer_pinned (bh)) {
fs/gfs2/super.c +1 -1
@@ -557,7 +557,7 @@ static void gfs2_dirty_inode(struct inode *inode, int flags) }else if (WARN_ON_ONCE (ip->i_gl->gl_state !=LM_ST_EXCLUSIVE ))return ; -if (current->journal_info ==NULL ) { +if (!current_trans ()) { ret =gfs2_trans_begin (sdp,RES_DINODE ,0 );if (ret) {fs_err (sdp,"dirty_inode: gfs2_trans_begin %d\n" , ret);
fs/gfs2/trans.c +8 -8
@@ -43,8 +43,8 @@ int __gfs2_trans_begin(struct gfs2_trans *tr, struct gfs2_sbd *sdp, {unsigned int extra_revokes; -if (current->journal_info) { -gfs2_print_trans (sdp, current->journal_info); +if (current_trans ()) { +gfs2_print_trans (sdp,current_trans ());BUG (); }BUG_ON (blocks ==0 && revokes ==0 );
@@ -101,7 +101,7 @@ int __gfs2_trans_begin(struct gfs2_trans *tr, struct gfs2_sbd *sdp,return -EROFS ; } - current->journal_info = tr; +set_current_trans (tr);return 0 ; }
@@ -123,10 +123,10 @@ int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,void gfs2_trans_end (struct gfs2_sbd *sdp) { -struct gfs2_trans *tr = current->journal_info; +struct gfs2_trans *tr =current_trans ();s64 nbuf; - current->journal_info =NULL ; +set_current_trans (NULL );if (!test_bit (TR_TOUCHED , &tr->tr_flags)) {gfs2_log_release_revokes (sdp, tr->tr_revokes);
@@ -191,7 +191,7 @@ static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,*/ void gfs2_trans_add_data (struct gfs2_glock *gl,struct buffer_head *bh) { -struct gfs2_trans *tr = current->journal_info; +struct gfs2_trans *tr =current_trans ();struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;struct gfs2_bufdata *bd;
@@ -232,7 +232,7 @@ void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;struct gfs2_bufdata *bd;struct gfs2_meta_header *mh; -struct gfs2_trans *tr = current->journal_info; +struct gfs2_trans *tr =current_trans ();enum gfs2_freeze_state state =atomic_read (&sdp->sd_freeze_state);lock_buffer (bh);
@@ -288,7 +288,7 @@ void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)void gfs2_trans_add_revoke (struct gfs2_sbd *sdp,struct gfs2_bufdata *bd) { -struct gfs2_trans *tr = current->journal_info; +struct gfs2_trans *tr =current_trans ();BUG_ON (!list_empty (&bd->bd_list));gfs2_add_revoke (sdp, bd);
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH 4/6 gfs2: Encode glock holding and retry flags in journal_info
In reply to Andreas GruenbacherUse the lowest two bits in current->journal_info to encode when we're holding a glock and when an operation holding a glock needs to be retried.
--- fs/gfs2/incore.h | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-)
fs/gfs2/incore.h +33 -2
@@ -871,14 +871,45 @@ static inline unsigned gfs2_max_stuffed_size(const struct gfs2_inode *ip) return GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize - sizeof(struct gfs2_dinode); } +/* +* Transactions are always memory aligned, so we use bit 0 of +* current->journal_info to indicate when we're holding a glock and so taking +* random additional glocks might deadlock, and bit 1 to indicate when such an +* operation needs to be retried after dropping and re-acquiring that "outer" +* glock. +*/ +static inline struct gfs2_trans *current_trans (void ) { -return current->journal_info; +return (void *)((long )current->journal_info & ~3 ); }static inline void set_current_trans (struct gfs2_trans *tr) { - current->journal_info = tr; +long flags = (long )current->journal_info &3 ; + current->journal_info = (void *)((long )tr | flags); +} + +static inline bool current_holds_glock (void ) +{ +return (long )current->journal_info &1 ; +} + +static inline bool current_needs_retry (void ) +{ +return (long )current->journal_info &2 ; +} + +static inline void set_current_holds_glock (bool b) +{ + current->journal_info = + (void *)(((long )current->journal_info & ~1 ) | b); +} + +static inline void set_current_needs_retry (bool b) +{ + current->journal_info = + (void *)(((long )current->journal_info & ~2 ) | (b <<1 )); }#endif /* __INCORE_DOT_H__ */
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH 5/6 gfs2: Add LM_FLAG_OUTER glock holder flag
In reply to Andreas GruenbacherWhen a glock holder has the LM_FLAG_OUTER flag set, we set the current_holds_glock() flag upon taking the lock. With that flag set, we can then recognize when trying to take an "inner" glock and react accordingly.
--- fs/gfs2/glock.c | 12 ++++++++++++ fs/gfs2/glock.h | 13 ++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-)
fs/gfs2/glock.c +12 -0
@@ -1427,6 +1427,11 @@ int gfs2_glock_nq(struct gfs2_holder *gh)if (glock_blocked_by_withdraw (gl) && !(gh->gh_flags &LM_FLAG_NOEXP ))return -EIO ; +if (gh->gh_flags &LM_FLAG_OUTER ) { +BUG_ON (current_holds_glock ()); +set_current_holds_glock (true); + } +if (test_bit (GLF_LRU , &gl->gl_flags))gfs2_glock_remove_from_lru (gl);
@@ -1514,6 +1519,11 @@ void gfs2_glock_dq(struct gfs2_holder *gh)__gfs2_glock_queue_work (gl, delay); }spin_unlock (&gl->gl_lockref.lock); + +if (gh->gh_flags &LM_FLAG_OUTER ) { +BUG_ON (!current_holds_glock ()); +set_current_holds_glock (false); + } }void gfs2_glock_dq_wait (struct gfs2_holder *gh)
@@ -2068,6 +2078,8 @@ static const char *hflags2str(char *buf, u16 flags, unsigned long iflags) *p++ ='p' ;if (flags &LM_FLAG_NODE_SCOPE ) *p++ ='n' ; +if (flags &LM_FLAG_OUTER ) + *p++ ='o' ;if (flags &GL_ASYNC ) *p++ ='a' ;if (flags &GL_EXACT )
fs/gfs2/glock.h +10 -3
@@ -94,6 +94,12 @@ static inline bool gfs2_holder_is_compatible(struct gfs2_holder *gh, int state)* This holder agrees to share the lock within this node. In other words, * the glock is held in EX mode according to DLM, but local holders on the * same node can share it. +* +* LM_FLAG_OUTER +* Use set_current_holds_glock() to indicate when the current task is holding +* this "upper" glock, and current_holds_glock() to detect when the current +* task is trying to take another glock. Used to prevent deadlocks involving +* the inode glock during page faults. */ #define LM_FLAG_TRY 0x0001
@@ -102,9 +108,10 @@ static inline bool gfs2_holder_is_compatible(struct gfs2_holder *gh, int state)#define LM_FLAG_ANY 0x0008#define LM_FLAG_PRIORITY 0x0010#define LM_FLAG_NODE_SCOPE 0x0020 -#define GL_ASYNC 0x0040 -#define GL_EXACT 0x0080 -#define GL_SKIP 0x0100 +#define LM_FLAG_OUTER 0x0040 +#define GL_ASYNC 0x0080 +#define GL_EXACT 0x0100 +#define GL_SKIP 0x0200#define GL_NOCACHE 0x0400/*
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH 6/6 gfs2: Fix mmap + page fault deadlocks (part 2)
In reply to Andreas GruenbacherNow that we handle self-recursion on the inode glock in gfs2_fault and gfs2_page_mkwrite, we need to take care of more complex deadlock scenarios like the following (example by Jan Kara):
Two independent processes P1, P2. Two files F1, F2, and two mappings M1, M2 where M1 is a mapping of F1, M2 is a mapping of F2. Now P1 does DIO to F1 with M2 as a buffer, P2 does DIO to F2 with M1 as a buffer. They can race like:
P1 P2read() read()
gfs2_file_read_iter() gfs2_file_read_iter()
gfs2_file_direct_read() gfs2_file_direct_read()
locks glock of F1 locks glock of F2
iomap_dio_rw() iomap_dio_rw()
bio_iov_iter_get_pages() bio_iov_iter_get_pages()
<fault in M2> <fault in M1>
gfs2_fault() gfs2_fault()
tries to grab glock of F2 tries to grab glock of F1
Those kinds of scenarios are much harder to reproduce than self-recursion.
We deal with such situations by using the LM_FLAG_OUTER flag to mark "outer" glock taking. Then, when taking an "inner" glock, we use the LM_FLAG_TRY flag so that locking attempts that don't immediately succeed will be aborted. In case of a failed locking attempt, we "unroll" to where the "outer" glock was taken, drop the "outer" glock, and fault in the first offending user page. This will re-trigger the "inner" locking attempt but without the LM_FLAG_TRY flag. Once that has happened, we re-acquire the "outer" glock and retry the original operation.
--- fs/gfs2/bmap.c | 3 ++- fs/gfs2/file.c | 62 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 7 deletions(-)
fs/gfs2/bmap.c +2 -1
@@ -967,7 +967,8 @@ static int gfs2_write_lock(struct inode *inode)struct gfs2_sbd *sdp =GFS2_SB (inode);int error; -gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE ,0 , &ip->i_gh); +gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE ,LM_FLAG_OUTER , + &ip->i_gh); error =gfs2_glock_nq (&ip->i_gh);if (error) goto out_uninit;
fs/gfs2/file.c +56 -6
145 lines not on this page · raw message
Signature · 1 line
2.26.3
Jan Kara
reviewerRe: [PATCH 6/6] gfs2: Fix mmap + page fault deadlocks (part 2)
Quoted 34 lines · Andreas Gruenbacher
Now that we handle self-recursion on the inode glock in
gfs2_faultandgfs2_page_mkwrite, we need to take care of more complex deadlock scenarios like the following (example by Jan Kara):Two independent processes P1, P2. Two files F1, F2, and two mappings M1, M2 where M1 is a mapping of F1, M2 is a mapping of F2. Now P1 does DIO to F1 with M2 as a buffer, P2 does DIO to F2 with M1 as a buffer. They can race like:
P1 P2
read()read()
gfs2_file_read_iter()gfs2_file_read_iter()
gfs2_file_direct_read()gfs2_file_direct_read()
locks glock of F1 locks glock of F2
iomap_dio_rw()iomap_dio_rw()
bio_iov_iter_get_pages()bio_iov_iter_get_pages()
<fault in M2> <fault in M1>
gfs2_fault()gfs2_fault()
tries to grab glock of F2 tries to grab glock of F1Those kinds of scenarios are much harder to reproduce than self-recursion.
We deal with such situations by using the
LM_FLAG_OUTERflag to mark "outer" glock taking. Then, when taking an "inner" glock, we use theLM_FLAG_TRYflag so that locking attempts that don't immediately succeed will be aborted. In case of a failed locking attempt, we "unroll" to where the "outer" glock was taken, drop the "outer" glock, and fault in the first offending user page. This will re-trigger the "inner" locking attempt but without theLM_FLAG_TRYflag. Once that has happened, we re-acquire the "outer" glock and retry the original operation.Reported-by Jan Kara <[email protected]>Signed-off-by Andreas Gruenbacher <[email protected]>
...
Quoted 17 lines
fs/gfs2/file.c +3
-1@@ -431,21 +431,30 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)vm_fault_t ret =VM_FAULT_LOCKED ;struct gfs2_holder gh;unsigned int length; +u16 flags =0 ;loff_t size;int err;sb_start_pagefault (inode->i_sb); -gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE ,0 , &gh); +if (current_holds_glock ()) + flags |=LM_FLAG_TRY ;
fs/gfs2/file.c static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf) · line 451
Quoted 10 lines
fs/gfs2/file.c +6
-0+ +gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE , flags, &gh);if (likely (!outer_gh)) { err =gfs2_glock_nq (&gh);if (err) { ret =block_page_mkwrite_return (err); +if (err ==GLR_TRYFAILED ) { +set_current_needs_retry (true); + ret =VM_FAULT_SIGBUS ; + }
I've checked to make sure but do_user_addr_fault() indeed calls do_sigbus() which raises the SIGBUS signal. So if the application does not ignore SIGBUS, your retry will be visible to the application and can cause all sorts of interesting results... So you probably need to add a new VM_FAULT_ return code that will behave like VM_FAULT_SIGBUS except it will not raise the signal.
Otherwise it seems to me your approach should work.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Andreas Gruenbacher
maintainerRe: [PATCH 6/6] gfs2: Fix mmap + page fault deadlocks (part 2)
Quoted 62 lines · Jan Kara
Quoted 34 lines · Andreas Gruenbacher
Now that we handle self-recursion on the inode glock in
gfs2_faultandgfs2_page_mkwrite, we need to take care of more complex deadlock scenarios like the following (example by Jan Kara):Two independent processes P1, P2. Two files F1, F2, and two mappings M1, M2 where M1 is a mapping of F1, M2 is a mapping of F2. Now P1 does DIO to F1 with M2 as a buffer, P2 does DIO to F2 with M1 as a buffer. They can race like:
P1 P2
read()read()
gfs2_file_read_iter()gfs2_file_read_iter()
gfs2_file_direct_read()gfs2_file_direct_read()
locks glock of F1 locks glock of F2
iomap_dio_rw()iomap_dio_rw()
bio_iov_iter_get_pages()bio_iov_iter_get_pages()
<fault in M2> <fault in M1>
gfs2_fault()gfs2_fault()
tries to grab glock of F2 tries to grab glock of F1Those kinds of scenarios are much harder to reproduce than self-recursion.
We deal with such situations by using the
LM_FLAG_OUTERflag to mark "outer" glock taking. Then, when taking an "inner" glock, we use theLM_FLAG_TRYflag so that locking attempts that don't immediately succeed will be aborted. In case of a failed locking attempt, we "unroll" to where the "outer" glock was taken, drop the "outer" glock, and fault in the first offending user page. This will re-trigger the "inner" locking attempt but without theLM_FLAG_TRYflag. Once that has happened, we re-acquire the "outer" glock and retry the original operation.Reported-by Jan Kara <[email protected]>Signed-off-by Andreas Gruenbacher <[email protected]>...
Quoted 24 lines
fs/gfs2/file.c +6
-1@@ -431,21 +431,30 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)vm_fault_t ret =VM_FAULT_LOCKED ;struct gfs2_holder gh;unsigned int length; +u16 flags =0 ;loff_t size;int err;sb_start_pagefault (inode->i_sb); -gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE ,0 , &gh); +if (current_holds_glock ()) + flags |=LM_FLAG_TRY ; + +gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE , flags, &gh);if (likely (!outer_gh)) { err =gfs2_glock_nq (&gh);if (err) { ret =block_page_mkwrite_return (err); +if (err ==GLR_TRYFAILED ) {
Quoted 8 lines
fs/gfs2/file.c static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf) · line 451
Quoted 3 lines
fs/gfs2/file.c +3
-0+set_current_needs_retry (true); + ret =VM_FAULT_SIGBUS ; + }I've checked to make sure but
do_user_addr_fault()indeed callsdo_sigbus()which raises the SIGBUS signal. So if the application does not ignore SIGBUS, your retry will be visible to the application and can cause all sorts of interesting results...
I would have noticed that, but no SIGBUS signals were actually delivered. So we probably end up in kernelmode_fixup_or_oops() when in kernel mode, which just does nothing in that case.
Andy Lutomirski, you've been involved with this, could you please shed some light?
Quoted 3 lines
So you probably need to add a new
VM_FAULT_
return code that will behave likeVM_FAULT_SIGBUSexcept it will not raise the signal.
A new VM_FAULT_* flag might make the code easier to read, but I don't know if we can have one.
Quoted 1 line
Otherwise it seems to me your approach should work.
Thanks a lot,
Andreas
Jan Kara
reviewerRe: [PATCH 6/6] gfs2: Fix mmap + page fault deadlocks (part 2)
Quoted 72 lines · Andreas Gruenbacher
Quoted 62 lines · Jan Kara
Quoted 34 lines · Andreas Gruenbacher
Now that we handle self-recursion on the inode glock in
gfs2_faultandgfs2_page_mkwrite, we need to take care of more complex deadlock scenarios like the following (example by Jan Kara):Two independent processes P1, P2. Two files F1, F2, and two mappings M1, M2 where M1 is a mapping of F1, M2 is a mapping of F2. Now P1 does DIO to F1 with M2 as a buffer, P2 does DIO to F2 with M1 as a buffer. They can race like:
P1 P2
read()read()
gfs2_file_read_iter()gfs2_file_read_iter()
gfs2_file_direct_read()gfs2_file_direct_read()
locks glock of F1 locks glock of F2
iomap_dio_rw()iomap_dio_rw()
bio_iov_iter_get_pages()bio_iov_iter_get_pages()
<fault in M2> <fault in M1>
gfs2_fault()gfs2_fault()
tries to grab glock of F2 tries to grab glock of F1Those kinds of scenarios are much harder to reproduce than self-recursion.
We deal with such situations by using the
LM_FLAG_OUTERflag to mark "outer" glock taking. Then, when taking an "inner" glock, we use theLM_FLAG_TRYflag so that locking attempts that don't immediately succeed will be aborted. In case of a failed locking attempt, we "unroll" to where the "outer" glock was taken, drop the "outer" glock, and fault in the first offending user page. This will re-trigger the "inner" locking attempt but without theLM_FLAG_TRYflag. Once that has happened, we re-acquire the "outer" glock and retry the original operation.Reported-by Jan Kara <[email protected]>Signed-off-by Andreas Gruenbacher <[email protected]>...
Quoted 24 lines
fs/gfs2/file.c +6
-1@@ -431,21 +431,30 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)vm_fault_t ret =VM_FAULT_LOCKED ;struct gfs2_holder gh;unsigned int length; +u16 flags =0 ;loff_t size;int err;sb_start_pagefault (inode->i_sb); -gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE ,0 , &gh); +if (current_holds_glock ()) + flags |=LM_FLAG_TRY ; + +gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE , flags, &gh);if (likely (!outer_gh)) { err =gfs2_glock_nq (&gh);if (err) { ret =block_page_mkwrite_return (err); +if (err ==GLR_TRYFAILED ) {Quoted 8 lines
fs/gfs2/file.c static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf) · line 451
Quoted 3 lines
fs/gfs2/file.c +3
-0+set_current_needs_retry (true); + ret =VM_FAULT_SIGBUS ; + }I've checked to make sure but
do_user_addr_fault()indeed callsdo_sigbus()which raises the SIGBUS signal. So if the application does not ignore SIGBUS, your retry will be visible to the application and can cause all sorts of interesting results...
Quoted 3 lines
I would have noticed that, but no SIGBUS signals were actually delivered. So we probably end up in
kernelmode_fixup_or_oops()when in kernel mode, which just does nothing in that case.
Hum, but how would we get there? I don't think fatal_signal_pending() would return true yet...
Quoted 6 lines
Quoted 3 lines
So you probably need to add a new
VM_FAULT_
return code that will behave likeVM_FAULT_SIGBUSexcept it will not raise the signal.A new VM_FAULT_* flag might make the code easier to read, but I don't know if we can have one.
Well, this is kernel-internal API and there's still plenty of space in vm_fault_reason.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Andreas Gruenbacher
maintainerRe: [PATCH 6/6] gfs2: Fix mmap + page fault deadlocks (part 2)
Quoted 73 lines · Jan Kara
Quoted 72 lines · Andreas Gruenbacher
Quoted 70 lines · Jan Kara
Quoted 34 lines · Andreas Gruenbacher
Now that we handle self-recursion on the inode glock in
gfs2_faultandgfs2_page_mkwrite, we need to take care of more complex deadlock scenarios like the following (example by Jan Kara):Two independent processes P1, P2. Two files F1, F2, and two mappings M1, M2 where M1 is a mapping of F1, M2 is a mapping of F2. Now P1 does DIO to F1 with M2 as a buffer, P2 does DIO to F2 with M1 as a buffer. They can race like:
P1 P2
read()read()
gfs2_file_read_iter()gfs2_file_read_iter()
gfs2_file_direct_read()gfs2_file_direct_read()
locks glock of F1 locks glock of F2
iomap_dio_rw()iomap_dio_rw()
bio_iov_iter_get_pages()bio_iov_iter_get_pages()
<fault in M2> <fault in M1>
gfs2_fault()gfs2_fault()
tries to grab glock of F2 tries to grab glock of F1Those kinds of scenarios are much harder to reproduce than self-recursion.
We deal with such situations by using the
LM_FLAG_OUTERflag to mark "outer" glock taking. Then, when taking an "inner" glock, we use theLM_FLAG_TRYflag so that locking attempts that don't immediately succeed will be aborted. In case of a failed locking attempt, we "unroll" to where the "outer" glock was taken, drop the "outer" glock, and fault in the first offending user page. This will re-trigger the "inner" locking attempt but without theLM_FLAG_TRYflag. Once that has happened, we re-acquire the "outer" glock and retry the original operation.Reported-by Jan Kara <[email protected]>Signed-off-by Andreas Gruenbacher <[email protected]>...
Quoted 17 lines
fs/gfs2/file.c +3
-1@@ -431,21 +431,30 @@ static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf)vm_fault_t ret =VM_FAULT_LOCKED ;struct gfs2_holder gh;unsigned int length; +u16 flags =0 ;loff_t size;int err;sb_start_pagefault (inode->i_sb); -gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE ,0 , &gh); +if (current_holds_glock ()) + flags |=LM_FLAG_TRY ;fs/gfs2/file.c static vm_fault_t gfs2_page_mkwrite(struct vm_fault *vmf) · line 451
Quoted 10 lines
fs/gfs2/file.c +6
-0+ +gfs2_holder_init (ip->i_gl,LM_ST_EXCLUSIVE , flags, &gh);if (likely (!outer_gh)) { err =gfs2_glock_nq (&gh);if (err) { ret =block_page_mkwrite_return (err); +if (err ==GLR_TRYFAILED ) { +set_current_needs_retry (true); + ret =VM_FAULT_SIGBUS ; + }I've checked to make sure but
do_user_addr_fault()indeed callsdo_sigbus()which raises the SIGBUS signal. So if the application does not ignore SIGBUS, your retry will be visible to the application and can cause all sorts of interesting results...
Quoted 6 lines
Quoted 3 lines
I would have noticed that, but no SIGBUS signals were actually delivered. So we probably end up in
kernelmode_fixup_or_oops()when in kernel mode, which just does nothing in that case.Hum, but how would we get there? I don't think
fatal_signal_pending()would return true yet...
Hmm, right ...
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
So you probably need to add a new
VM_FAULT_
return code that will behave likeVM_FAULT_SIGBUSexcept it will not raise the signal.A new VM_FAULT_* flag might make the code easier to read, but I don't know if we can have one.
Well, this is kernel-internal API and there's still plenty of space in
vm_fault_reason.
That's in the context of the page fault. The other issue is how to propagate that out through iov_iter_fault_in_readable -> fault_in_pages_readable -> __get_user, for example. I don't think there's much of a chance to get an additional error code out of __get_user and __put_user.
Thanks,
Andreas
Jan Kara
reviewerRe: [PATCH 6/6] gfs2: Fix mmap + page fault deadlocks (part 2)
Quoted 9 lines · Andreas Gruenbacher
Quoted 8 lines · Jan Kara
Quoted 6 lines · Andreas Gruenbacher
Quoted 3 lines
So you probably need to add a new
VM_FAULT_
return code that will behave likeVM_FAULT_SIGBUSexcept it will not raise the signal.A new VM_FAULT_* flag might make the code easier to read, but I don't know if we can have one.
Quoted 8 lines
Quoted 2 lines
Well, this is kernel-internal API and there's still plenty of space in
vm_fault_reason.That's in the context of the page fault. The other issue is how to propagate that out through
iov_iter_fault_in_readable->fault_in_pages_readable->__get_user, for example. I don't think there's much of a chance to get an additional error code out of__get_userand__put_user.
Yes, at that level we'd get EFAULT as in any other case. Really the only difference of the new VM_FAULT_ error code from a case of "standard" error and VM_FAULT_SIGBUS would be not raising the signal.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR