gfs2: keep glocks alive while they have active buffers
Andreas Gruenbacher
maintainerPATCH 1/1 gfs2: keep glocks alive while they have active buffers
It turns out that taking an extra inode glock reference in gfs2_unpin() is not enough to keep the glock alive because the inode may already have been evicted. Instead, we must take that extra reference in gfs2_pin() already.
To keep track of the number of active buffers, introduce an additional gl_bufdata_count counter. This counter includes all buffers that are on sd_log_revokes, in sd_log_tr, or in any of the transactions on sd_ail1_list or sd_ail2_list.
Grab an extra glock reference when gl_bufdata_count reaches 1, and drop that extra reference when that counter drops to 0. This replaces the extra glock reference taking tied to the gl_revokes and gl_ail_count.
Fixes 9287c6452d2b ("gfs2: Fix occasional glock use-after-free").
Fixes 1c453efdaafa ("gfs2: keep glocks alive while AIL entries exist")
Signed-off-by Andreas Gruenbacher <[email protected]>
--- fs/gfs2/incore.h | 26 ++++++++++++++++++++++++++ fs/gfs2/log.c | 35 ++++++++++++++++++++++++++--------- fs/gfs2/lops.c | 10 ++++++++-- fs/gfs2/main.c | 1 + 4 files changed, 61 insertions(+), 11 deletions(-)
fs/gfs2/incore.h +26 -0
@@ -137,6 +137,15 @@ TAS_BUFFER_FNS(Pinned, pinned) BUFFER_FNS(Escaped, escaped)TAS_BUFFER_FNS (Escaped, escaped) +/** +* struct gfs2_bufdata +* +* Bufdata objects normally remain attached to their corresponding buffer head +* (bd->bd_bh == bh; bh->b_private == bd), but when they are turned into +* revokes, they are detached from bh and from their transaction and put onto +* the sd_log_revokes list. + */ +struct gfs2_bufdata {struct buffer_head *bd_bh;struct gfs2_glock *bd_gl;
@@ -330,6 +339,12 @@ enum { GLF_CANCELING = 21, }; +/** +* struct gfs2_glock +* @gl_bufdata_count: The number of bufdata objects that refer to this glock. +* Bufdata objects refer to glocks via transactions (struct gfs_trans) and via +* the sd_log_revokes list. +*/ struct gfs2_glock {unsigned long gl_flags;/* GLF_... */ struct lm_lockname gl_name;
@@ -356,6 +371,7 @@ struct gfs2_glock {struct list_head gl_dead;struct list_head gl_ail_list; +atomic_t gl_bufdata_count;atomic_t gl_ail_count;atomic_t gl_revokes;struct delayed_work gl_work;
@@ -475,6 +491,16 @@ enum { TR_ONSTACK = 3, }; +/** +* struct gfs2_trans +* +* Uncommitted transactions refer to bufdata objects via the tr_buf and +* tr_databuf lists. Committed transactions refer to bufdata objects via +* tr_ail1_list and tr_ail2_list. A bufdata object cannot be both on tr_buf +* and tr_databuf, and it cannot be both on tr_ail1_list and tr_ail2_list at +* the same time. It can be part of an uncommitted and a committed transaction +* at the same time, though. +*/ struct gfs2_trans {unsigned long tr_ip;
fs/gfs2/log.c +26 -9
@@ -65,25 +65,39 @@ unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct) }/** -* gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters +* __gfs2_remove_from_ail - Remove an entry from the ail lists * @bd: The gfs2_bufdata to remove * * The ail lock _must_ be held when calling this function * */ -static void gfs2_remove_from_ail (struct gfs2_bufdata *bd) +static void __gfs2_remove_from_ail (struct gfs2_bufdata *bd) {struct gfs2_glock *gl = bd->bd_gl; bd->bd_tr =NULL ;list_del_init (&bd->bd_ail_st_list);list_del_init (&bd->bd_ail_gl_list); -if (atomic_dec_and_test (&gl->gl_ail_count)) -gfs2_glock_put_async (gl); +atomic_dec (&gl->gl_ail_count);brelse (bd->bd_bh); } +static void gfs2_remove_from_ail (struct gfs2_bufdata *bd) +{ +__gfs2_remove_from_ail (bd); +if (list_empty (&bd->bd_list)) { +struct gfs2_glock *gl = bd->bd_gl; + +/* +* bd is not part of a uncommitted transaction, and it is still +* attached to its bh. +*/ +if (atomic_dec_and_test (&gl->gl_bufdata_count)) +gfs2_glock_put_async (gl); + } +} +/** * gfs2_ail1_start_one - Start I/O on a transaction * @sdp: The superblock
@@ -765,11 +779,10 @@ void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)struct gfs2_glock *gl = bd->bd_gl; sdp->sd_log_num_revoke++; -if (atomic_inc_return (&gl->gl_revokes) ==1 ) -gfs2_glock_hold (gl); +atomic_inc (&gl->gl_revokes); bh->b_private =NULL ; bd->bd_blkno = bh->b_blocknr; -gfs2_remove_from_ail (bd);/* drops ref on bh */ +__gfs2_remove_from_ail (bd);/* drops ref on bh */ bd->bd_bh =NULL ;set_bit (GLF_LFLUSH , &gl->gl_flags);list_add (&bd->bd_list, &sdp->sd_log_revokes);
@@ -780,10 +793,10 @@ void gfs2_glock_remove_revoke(struct gfs2_bufdata *bd)struct gfs2_glock *gl = bd->bd_gl;list_del_init (&bd->bd_list); -if (atomic_dec_and_test (&gl->gl_revokes)) { +if (atomic_dec_and_test (&gl->gl_revokes))clear_bit (GLF_LFLUSH , &gl->gl_flags); +if (atomic_dec_and_test (&gl->gl_bufdata_count))gfs2_glock_put_async (gl); - }kmem_cache_free (gfs2_bufdata_cachep, bd); }
@@ -1044,6 +1057,10 @@ void gfs2_remove_from_journal(struct buffer_head *bh, int meta)else gfs2_remove_from_ail (bd); }else if (was_pinned) { +struct gfs2_glock *gl = bd->bd_gl; + +if (atomic_dec_and_test (&gl->gl_bufdata_count)) +gfs2_glock_put_async (gl); bh->b_private =NULL ;kmem_cache_free (gfs2_bufdata_cachep, bd); } else if (!list_empty(&bd->bd_ail_st_list) &&
fs/gfs2/lops.c +8 -2
@@ -57,6 +57,13 @@ void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)spin_lock (&sdp->sd_ail_lock);if (bd->bd_tr)list_move (&bd->bd_ail_st_list, &bd->bd_tr->tr_ail2_list); +else { +struct gfs2_glock *gl = bd->bd_gl; + +/* bd is attached to bh, so it cannot be a revoke. */ +if (atomic_inc_return (&gl->gl_bufdata_count) ==1 ) +gfs2_glock_hold (gl); + }spin_unlock (&sdp->sd_ail_lock);get_bh (bh);atomic_inc (&sdp->sd_log_pinned);
@@ -121,8 +128,7 @@ static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh, }else {struct gfs2_glock *gl = bd->bd_gl;list_add (&bd->bd_ail_gl_list, &gl->gl_ail_list); -if (atomic_inc_return (&gl->gl_ail_count) ==1 ) -gfs2_glock_hold (gl); +atomic_inc (&gl->gl_ail_count); } bd->bd_tr = tr;list_add (&bd->bd_ail_st_list, &tr->tr_ail1_list);
fs/gfs2/main.c +1 -0
@@ -53,6 +53,7 @@ static void gfs2_init_glock_once(void *foo)INIT_LIST_HEAD (&gl->gl_holders);INIT_LIST_HEAD (&gl->gl_ail_list); +atomic_set (&gl->gl_bufdata_count,0 );atomic_set (&gl->gl_ail_count,0 );atomic_set (&gl->gl_revokes,0 ); }
Signature · 1 line
2.56.1