xfs: fix AIL push use-after-free during shutdown
Subsystems (2)
v4 cover xfs: fix AIL push use-after-free during shutdown
When a filesystem is shut down, background inode reclaim and the xfsaild can race to abort and free dirty inodes. Since commit 90c60e164012 ("xfs: xfs_iflush() is no longer necessary"), xfs_inode_item_push() no longer holds ILOCK_SHARED while flushing, removing the protection that prevented the inode from being reclaimed during the flush.
This results in use-after-free when dereferencing log items after iop_push() returns, or when reacquiring the AIL lock via lip->li_ailp.
This series fixes the issue by:
1. Reordering unmount to stop reclaim and inodegc before pushing the AIL
2. Capturing log item fields before push callbacks for tracepoints
3. Saving the ailp pointer before dropping the AIL lock
4. Factoring the push loop into a helper for readability (non-bugfix)
Changes in v4:
- Fixed unmount ordering: xfs_inodegc_stop before cancel_delayed_work_sync to prevent inodegc from re-queuing m_reclaim_work as suggested by Darrick J. Wong
- Updated xfs_unmount_flush_inodes function comment to reflect new ordering
- Reworked patch ordering so bugfix patches (1-3) do not depend on the refactoring patch, reducing stable backport burden
- Added comment at xfsaild_push_item call site documenting that the log item must not be dereferenced after the call
- Moved refactoring to patch 4 without Cc: stable
- Link to v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Split into 4 patches as suggested by Dave Chinner
- Moved UAF-unsafe point comments to after xfs_buf_relse()
- Passed ailp instead of dev to tracepoints
- Moved xfs_ail_push_class definition after xfs_log_item_class events
- Factored xfsaild_push() loop body into xfsaild_process_logitem()
- Added xfsaild_push_item() header comment describing post-return lifetime
- Link to v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Reordered xfs_unmount_flush_inodes() to stop reclaim before pushing AIL suggested by Dave Chinner
- Introduced xfs_ail_push_class trace event to avoid dereferencing freed log items in tracepoints
- Added comments documenting that log items must not be referenced after iop_push() returns
- Saved ailp pointer in local variables in push functions
- Link to v1: https://lore.kernel.org/all/[email protected]/
Yuto Ohnuki (4):
xfs: stop reclaim before pushing AIL during unmount
xfs: avoid dereferencing log items after push callbacks
xfs: save ailp before dropping the AIL lock in push callbacks
xfs: refactor xfsaild_push loop into helper
fs/xfs/xfs_dquot_item.c | 9 ++- fs/xfs/xfs_inode_item.c | 9 ++- fs/xfs/xfs_mount.c | 7 ++- fs/xfs/xfs_trace.h | 36 ++++++++++-- fs/xfs/xfs_trans_ail.c | 127 ++++++++++++++++++++++++---------------- 5 files changed, 125 insertions(+), 63 deletions(-)
Signature · 8 lines
2.50.1 Amazon Web Services EMEA SARL, 38 avenue John F. Kennedy, L-1855 Luxembourg, R.C.S. Luxembourg B186284 Amazon Web Services EMEA SARL, Irish Branch, One Burlington Plaza, Burlington Road, Dublin 4, Ireland, branch registration number 908705
PATCH v4 1/4 xfs: stop reclaim before pushing AIL during unmount
The unmount sequence in xfs_unmount_flush_inodes() pushed the AIL while background reclaim and inodegc are still running. This is broken independently of any use-after-free issues - background reclaim and inodegc should not be running while the AIL is being pushed during unmount, as inodegc can dirty and insert inodes into the AIL during the flush, and background reclaim can race to abort and free dirty inodes.
Reorder xfs_unmount_flush_inodes() to stop inodegc and cancel background reclaim before pushing the AIL. Stop inodegc before cancelling m_reclaim_work because the inodegc worker can re-queue m_reclaim_work via xfs_inodegc_set_reclaimable.
--- fs/xfs/xfs_mount.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/xfs/xfs_mount.c +4 -3
@@ -608,8 +608,9 @@ xfs_unmount_check(* have been retrying in the background. This will prevent never-ending * retries in AIL pushing from hanging the unmount. * -* Finally, we can push the AIL to clean all the remaining dirty objects, then -* reclaim the remaining inodes that are still in memory at this point in time. +* Stop inodegc and background reclaim before pushing the AIL so that they +* are not running while the AIL is being flushed. Then push the AIL to +* clean all the remaining dirty objects and reclaim the remaining inodes. */ static void xfs_unmount_flush_inodes(
@@ -621,9 +622,9 @@ xfs_unmount_flush_inodes(xfs_set_unmounting (mp); -xfs_ail_push_all_sync (mp->m_ail);xfs_inodegc_stop (mp);cancel_delayed_work_sync (&mp->m_reclaim_work); +xfs_ail_push_all_sync (mp->m_ail);xfs_reclaim_inodes (mp);xfs_health_unmount (mp);xfs_healthmon_unmount (mp);
Signature · 8 lines
2.50.1 Amazon Web Services EMEA SARL, 38 avenue John F. Kennedy, L-1855 Luxembourg, R.C.S. Luxembourg B186284 Amazon Web Services EMEA SARL, Irish Branch, One Burlington Plaza, Burlington Road, Dublin 4, Ireland, branch registration number 908705
Re: [PATCH v4 1/4] xfs: stop reclaim before pushing AIL during unmount
Quoted 12 lines · Yuto Ohnuki
The unmount sequence in
xfs_unmount_flush_inodes()pushed the AIL while background reclaim and inodegc are still running. This is broken independently of any use-after-free issues - background reclaim and inodegc should not be running while the AIL is being pushed during unmount, as inodegc can dirty and insert inodes into the AIL during the flush, and background reclaim can race to abort and free dirty inodes.Reorder
xfs_unmount_flush_inodes()to stop inodegc and cancel background reclaim before pushing the AIL. Stop inodegc before cancellingm_reclaim_workbecause the inodegc worker can re-queuem_reclaim_workviaxfs_inodegc_set_reclaimable.
Quoted 5 lines
Reported-by [email protected]Fixes 90c60e164012 ("xfs: xfs_iflush() is no longer necessary")Cc <[email protected]> # v5.9Signed-off-by Yuto Ohnuki <[email protected]>
Looks good now,
--D
Quoted 44 lines
--- fs/xfs/xfs_mount.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)fs/xfs/xfs_mount.c +4
-3@@ -608,8 +608,9 @@ xfs_unmount_check(* have been retrying in the background. This will prevent never-ending * retries in AIL pushing from hanging the unmount. * -* Finally, we can push the AIL to clean all the remaining dirty objects, then -* reclaim the remaining inodes that are still in memory at this point in time. +* Stop inodegc and background reclaim before pushing the AIL so that they +* are not running while the AIL is being flushed. Then push the AIL to +* clean all the remaining dirty objects and reclaim the remaining inodes. */ static void xfs_unmount_flush_inodes(@@ -621,9 +622,9 @@ xfs_unmount_flush_inodes(xfs_set_unmounting (mp); -xfs_ail_push_all_sync (mp->m_ail);xfs_inodegc_stop (mp);cancel_delayed_work_sync (&mp->m_reclaim_work); +xfs_ail_push_all_sync (mp->m_ail);xfs_reclaim_inodes (mp);xfs_health_unmount (mp);xfs_healthmon_unmount (mp);Signature · 8 lines
2.50.1 Amazon Web Services EMEA SARL, 38 avenue John F. Kennedy, L-1855 Luxembourg, R.C.S. Luxembourg B186284 Amazon Web Services EMEA SARL, Irish Branch, One Burlington Plaza, Burlington Road, Dublin 4, Ireland, branch registration number 908705
PATCH v4 2/4 xfs: avoid dereferencing log items after push callbacks
In reply to Yuto OhnukiAfter xfsaild_push_item() calls iop_push(), the log item may have been freed if the AIL lock was dropped during the push. Background inode reclaim or the dquot shrinker can free the log item while the AIL lock is not held, and the tracepoints in the switch statement dereference the log item after iop_push() returns.
Fix this by capturing the log item type, flags, and LSN before calling xfsaild_push_item(), and introducing a new xfs_ail_push_class trace event class that takes these pre-captured values and the ailp pointer instead of the log item pointer.
--- fs/xfs/xfs_trace.h | 36 ++++++++++++++++++++++++++++++++---- fs/xfs/xfs_trans_ail.c | 26 +++++++++++++++++++------- 2 files changed, 51 insertions(+), 11 deletions(-)
fs/xfs/xfs_trace.h +32 -4
@@ -56,6 +56,7 @@#include <linux/tracepoint.h> struct xfs_agf ; +struct xfs_ail ;struct xfs_alloc_arg ;struct xfs_attr_list_context ;struct xfs_buf_log_item ;
@@ -1650,16 +1651,43 @@ TRACE_EVENT(xfs_log_force,DEFINE_EVENT (xfs_log_item_class, name, \ TP_PROTO(struct xfs_log_item *lip), \ TP_ARGS(lip)) -DEFINE_LOG_ITEM_EVENT (xfs_ail_push); -DEFINE_LOG_ITEM_EVENT (xfs_ail_pinned); -DEFINE_LOG_ITEM_EVENT (xfs_ail_locked); -DEFINE_LOG_ITEM_EVENT (xfs_ail_flushing);DEFINE_LOG_ITEM_EVENT (xfs_cil_whiteout_mark);DEFINE_LOG_ITEM_EVENT (xfs_cil_whiteout_skip);DEFINE_LOG_ITEM_EVENT (xfs_cil_whiteout_unpin);DEFINE_LOG_ITEM_EVENT (xlog_ail_insert_abort);DEFINE_LOG_ITEM_EVENT (xfs_trans_free_abort); +DECLARE_EVENT_CLASS (xfs_ail_push_class, + TP_PROTO(struct xfs_ail *ailp, uint type, unsigned long flags, xfs_lsn_t lsn), +TP_ARGS (ailp, type, flags, lsn), +TP_STRUCT__entry ( + __field(dev_t, dev) + __field(uint, type) + __field(unsigned long, flags) +__field (xfs_lsn_t, lsn) + ), +TP_fast_assign ( + __entry->dev = ailp->ail_log->l_mp->m_super->s_dev; + __entry->type = type; + __entry->flags = flags; + __entry->lsn = lsn; + ), +TP_printk ("dev %d:%d lsn %d/%d type %s flags %s" , +MAJOR (__entry->dev),MINOR (__entry->dev), +CYCLE_LSN (__entry->lsn),BLOCK_LSN (__entry->lsn), +__print_symbolic (__entry->type,XFS_LI_TYPE_DESC ), +__print_flags (__entry->flags,"|" ,XFS_LI_FLAGS )) +) + +#define DEFINE_AIL_PUSH_EVENT (name) \ +DEFINE_EVENT(xfs_ail_push_class, name, \ + TP_PROTO(struct xfs_ail *ailp, uint type, unsigned long flags, xfs_lsn_t lsn), \ + TP_ARGS(ailp, type, flags, lsn)) +DEFINE_AIL_PUSH_EVENT (xfs_ail_push); +DEFINE_AIL_PUSH_EVENT (xfs_ail_pinned); +DEFINE_AIL_PUSH_EVENT (xfs_ail_locked); +DEFINE_AIL_PUSH_EVENT (xfs_ail_flushing); + DECLARE_EVENT_CLASS(xfs_ail_class, TP_PROTO(struct xfs_log_item *lip, xfs_lsn_t old_lsn, xfs_lsn_t new_lsn), TP_ARGS(lip, old_lsn, new_lsn),
fs/xfs/xfs_trans_ail.c +19 -7
@@ -365,6 +365,12 @@ xfsaild_resubmit_item(return XFS_ITEM_SUCCESS ; } +/* +* Push a single log item from the AIL. +* +* @lip may have been released and freed by the time this function returns, +* so callers must not dereference the log item afterwards. +*/ static inline uint xfsaild_push_item( struct xfs_ail *ailp,
@@ -505,7 +511,10 @@ xfsaild_push( lsn = lip->li_lsn;while ((XFS_LSN_CMP (lip->li_lsn, ailp->ail_target) <=0 )) { -int lock_result; +int lock_result; +uint type = lip->li_type; +unsigned long flags = lip->li_flags; +xfs_lsn_t item_lsn = lip->li_lsn;if (test_bit (XFS_LI_FLUSHING , &lip->li_flags)) goto next_item;
@@ -514,14 +523,17 @@ xfsaild_push(* Note that iop_push may unlock and reacquire the AIL lock. We * rely on the AIL cursor implementation to be able to deal with * the dropped lock. +* +* The log item may have been freed by the push, so it must not +* be accessed or dereferenced below this line. */ lock_result =xfsaild_push_item (ailp, lip);switch (lock_result) {case XFS_ITEM_SUCCESS :XFS_STATS_INC (mp, xs_push_ail_success); -trace_xfs_ail_push (lip); +trace_xfs_ail_push (ailp, type, flags, item_lsn); - ailp->ail_last_pushed_lsn = lsn; + ailp->ail_last_pushed_lsn = item_lsn;break ;case XFS_ITEM_FLUSHING :
@@ -537,22 +549,22 @@ xfsaild_push(* AIL is being flushed. */ XFS_STATS_INC (mp, xs_push_ail_flushing); -trace_xfs_ail_flushing (lip); +trace_xfs_ail_flushing (ailp, type, flags, item_lsn); flushing++; - ailp->ail_last_pushed_lsn = lsn; + ailp->ail_last_pushed_lsn = item_lsn;break ;case XFS_ITEM_PINNED :XFS_STATS_INC (mp, xs_push_ail_pinned); -trace_xfs_ail_pinned (lip); +trace_xfs_ail_pinned (ailp, type, flags, item_lsn); stuck++; ailp->ail_log_flush++;break ;case XFS_ITEM_LOCKED :XFS_STATS_INC (mp, xs_push_ail_locked); -trace_xfs_ail_locked (lip); +trace_xfs_ail_locked (ailp, type, flags, item_lsn); stuck++;break ;
Signature · 8 lines
2.50.1 Amazon Web Services EMEA SARL, 38 avenue John F. Kennedy, L-1855 Luxembourg, R.C.S. Luxembourg B186284 Amazon Web Services EMEA SARL, Irish Branch, One Burlington Plaza, Burlington Road, Dublin 4, Ireland, branch registration number 908705
Re: [PATCH v4 2/4] xfs: avoid dereferencing log items after push callbacks
Quoted 11 lines · Yuto Ohnuki
After
xfsaild_push_item()callsiop_push(), the log item may have been freed if the AIL lock was dropped during the push. Background inode reclaim or the dquot shrinker can free the log item while the AIL lock is not held, and the tracepoints in the switch statement dereference the log item afteriop_push()returns.Fix this by capturing the log item type, flags, and LSN before calling
xfsaild_push_item(), and introducing a newxfs_ail_push_classtrace event class that takes these pre-captured values and the ailp pointer instead of the log item pointer.
Quoted 5 lines
Reported-by [email protected]Fixes 90c60e164012 ("xfs: xfs_iflush() is no longer necessary")Cc <[email protected]> # v5.9Signed-off-by Yuto Ohnuki <[email protected]>
That looks like a solid fix to me; thanks for working on this!
--D
Quoted 154 lines
--- fs/xfs/xfs_trace.h | 36 ++++++++++++++++++++++++++++++++---- fs/xfs/xfs_trans_ail.c | 26 +++++++++++++++++++------- 2 files changed, 51 insertions(+), 11 deletions(-)fs/xfs/xfs_trace.h +32
-4@@ -56,6 +56,7 @@#include <linux/tracepoint.h> struct xfs_agf ; +struct xfs_ail ;struct xfs_alloc_arg ;struct xfs_attr_list_context ;struct xfs_buf_log_item ;@@ -1650,16 +1651,43 @@ TRACE_EVENT(xfs_log_force,DEFINE_EVENT (xfs_log_item_class, name, \ TP_PROTO(struct xfs_log_item *lip), \ TP_ARGS(lip)) -DEFINE_LOG_ITEM_EVENT (xfs_ail_push); -DEFINE_LOG_ITEM_EVENT (xfs_ail_pinned); -DEFINE_LOG_ITEM_EVENT (xfs_ail_locked); -DEFINE_LOG_ITEM_EVENT (xfs_ail_flushing);DEFINE_LOG_ITEM_EVENT (xfs_cil_whiteout_mark);DEFINE_LOG_ITEM_EVENT (xfs_cil_whiteout_skip);DEFINE_LOG_ITEM_EVENT (xfs_cil_whiteout_unpin);DEFINE_LOG_ITEM_EVENT (xlog_ail_insert_abort);DEFINE_LOG_ITEM_EVENT (xfs_trans_free_abort); +DECLARE_EVENT_CLASS (xfs_ail_push_class, + TP_PROTO(struct xfs_ail *ailp, uint type, unsigned long flags, xfs_lsn_t lsn), +TP_ARGS (ailp, type, flags, lsn), +TP_STRUCT__entry ( + __field(dev_t, dev) + __field(uint, type) + __field(unsigned long, flags) +__field (xfs_lsn_t, lsn) + ), +TP_fast_assign ( + __entry->dev = ailp->ail_log->l_mp->m_super->s_dev; + __entry->type = type; + __entry->flags = flags; + __entry->lsn = lsn; + ), +TP_printk ("dev %d:%d lsn %d/%d type %s flags %s" , +MAJOR (__entry->dev),MINOR (__entry->dev), +CYCLE_LSN (__entry->lsn),BLOCK_LSN (__entry->lsn), +__print_symbolic (__entry->type,XFS_LI_TYPE_DESC ), +__print_flags (__entry->flags,"|" ,XFS_LI_FLAGS )) +) + +#define DEFINE_AIL_PUSH_EVENT (name) \ +DEFINE_EVENT(xfs_ail_push_class, name, \ + TP_PROTO(struct xfs_ail *ailp, uint type, unsigned long flags, xfs_lsn_t lsn), \ + TP_ARGS(ailp, type, flags, lsn)) +DEFINE_AIL_PUSH_EVENT (xfs_ail_push); +DEFINE_AIL_PUSH_EVENT (xfs_ail_pinned); +DEFINE_AIL_PUSH_EVENT (xfs_ail_locked); +DEFINE_AIL_PUSH_EVENT (xfs_ail_flushing); + DECLARE_EVENT_CLASS(xfs_ail_class, TP_PROTO(struct xfs_log_item *lip, xfs_lsn_t old_lsn, xfs_lsn_t new_lsn), TP_ARGS(lip, old_lsn, new_lsn),fs/xfs/xfs_trans_ail.c +19
-7@@ -365,6 +365,12 @@ xfsaild_resubmit_item(return XFS_ITEM_SUCCESS ; } +/* +* Push a single log item from the AIL. +* +* @lip may have been released and freed by the time this function returns, +* so callers must not dereference the log item afterwards. +*/ static inline uint xfsaild_push_item( struct xfs_ail *ailp,@@ -505,7 +511,10 @@ xfsaild_push( lsn = lip->li_lsn;while ((XFS_LSN_CMP (lip->li_lsn, ailp->ail_target) <=0 )) { -int lock_result; +int lock_result; +uint type = lip->li_type; +unsigned long flags = lip->li_flags; +xfs_lsn_t item_lsn = lip->li_lsn;if (test_bit (XFS_LI_FLUSHING , &lip->li_flags)) goto next_item;@@ -514,14 +523,17 @@ xfsaild_push(* Note that iop_push may unlock and reacquire the AIL lock. We * rely on the AIL cursor implementation to be able to deal with * the dropped lock. +* +* The log item may have been freed by the push, so it must not +* be accessed or dereferenced below this line. */ lock_result =xfsaild_push_item (ailp, lip);switch (lock_result) {case XFS_ITEM_SUCCESS :XFS_STATS_INC (mp, xs_push_ail_success); -trace_xfs_ail_push (lip); +trace_xfs_ail_push (ailp, type, flags, item_lsn); - ailp->ail_last_pushed_lsn = lsn; + ailp->ail_last_pushed_lsn = item_lsn;break ;case XFS_ITEM_FLUSHING :@@ -537,22 +549,22 @@ xfsaild_push(* AIL is being flushed. */ XFS_STATS_INC (mp, xs_push_ail_flushing); -trace_xfs_ail_flushing (lip); +trace_xfs_ail_flushing (ailp, type, flags, item_lsn); flushing++; - ailp->ail_last_pushed_lsn = lsn; + ailp->ail_last_pushed_lsn = item_lsn;break ;case XFS_ITEM_PINNED :XFS_STATS_INC (mp, xs_push_ail_pinned); -trace_xfs_ail_pinned (lip); +trace_xfs_ail_pinned (ailp, type, flags, item_lsn); stuck++; ailp->ail_log_flush++;break ;case XFS_ITEM_LOCKED :XFS_STATS_INC (mp, xs_push_ail_locked); -trace_xfs_ail_locked (lip); +trace_xfs_ail_locked (ailp, type, flags, item_lsn); stuck++;break ;Signature · 8 lines
2.50.1 Amazon Web Services EMEA SARL, 38 avenue John F. Kennedy, L-1855 Luxembourg, R.C.S. Luxembourg B186284 Amazon Web Services EMEA SARL, Irish Branch, One Burlington Plaza, Burlington Road, Dublin 4, Ireland, branch registration number 908705
PATCH v4 3/4 xfs: save ailp before dropping the AIL lock in push callbacks
In reply to Yuto OhnukiIn xfs_inode_item_push() and xfs_qm_dquot_logitem_push(), the AIL lock is dropped to perform buffer IO. Once the cluster buffer no longer protects the log item from reclaim, the log item may be freed by background reclaim or the dquot shrinker. The subsequent spin_lock() call dereferences lip->li_ailp, which is a use-after-free.
Fix this by saving the ailp pointer in a local variable while the AIL lock is held and the log item is guaranteed to be valid.
--- fs/xfs/xfs_dquot_item.c | 9 +++++++-- fs/xfs/xfs_inode_item.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-)
fs/xfs/xfs_dquot_item.c +7 -2
@@ -125,6 +125,7 @@ xfs_qm_dquot_logitem_push(struct xfs_dq_logitem *qlip =DQUOT_ITEM (lip);struct xfs_dquot *dqp = qlip->qli_dquot;struct xfs_buf *bp; +struct xfs_ail *ailp = lip->li_ailp;uint rval =XFS_ITEM_SUCCESS ;int error;
@@ -153,7 +154,7 @@ xfs_qm_dquot_logitem_push( goto out_unlock; } -spin_unlock (&lip->li_ailp->ail_lock); +spin_unlock (&ailp->ail_lock); error =xfs_dquot_use_attached_buf (dqp, &bp);if (error == -EAGAIN ) {
@@ -172,9 +173,13 @@ xfs_qm_dquot_logitem_push( rval =XFS_ITEM_FLUSHING ; }xfs_buf_relse (bp); +/* +* The buffer no longer protects the log item from reclaim, so +* do not reference lip after this point. +*/ out_relock_ail: -spin_lock (&lip->li_ailp->ail_lock); +spin_lock (&ailp->ail_lock); out_unlock:mutex_unlock (&dqp->q_qlock);return rval;
fs/xfs/xfs_inode_item.c +7 -2
@@ -746,6 +746,7 @@ xfs_inode_item_push(struct xfs_inode_log_item *iip =INODE_ITEM (lip);struct xfs_inode *ip = iip->ili_inode;struct xfs_buf *bp = lip->li_buf; +struct xfs_ail *ailp = lip->li_ailp;uint rval =XFS_ITEM_SUCCESS ;int error;
@@ -771,7 +772,7 @@ xfs_inode_item_push(if (!xfs_buf_trylock (bp))return XFS_ITEM_LOCKED ; -spin_unlock (&lip->li_ailp->ail_lock); +spin_unlock (&ailp->ail_lock);/* * We need to hold a reference for flushing the cluster buffer as it may
@@ -795,7 +796,11 @@ xfs_inode_item_push( rval =XFS_ITEM_LOCKED ; } -spin_lock (&lip->li_ailp->ail_lock); +/* +* The buffer no longer protects the log item from reclaim, so +* do not reference lip after this point. +*/ +spin_lock (&ailp->ail_lock);return rval; }
Signature · 8 lines
2.50.1 Amazon Web Services EMEA SARL, 38 avenue John F. Kennedy, L-1855 Luxembourg, R.C.S. Luxembourg B186284 Amazon Web Services EMEA SARL, Irish Branch, One Burlington Plaza, Burlington Road, Dublin 4, Ireland, branch registration number 908705
PATCH v4 4/4 xfs: refactor xfsaild_push loop into helper
In reply to Yuto OhnukiFactor the loop body of xfsaild_push() into a separate xfsaild_process_logitem() helper to improve readability.
This is a pure code movement with no functional change.
--- fs/xfs/xfs_trans_ail.c | 127 ++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 58 deletions(-)
fs/xfs/xfs_trans_ail.c +69 -58
@@ -464,6 +464,74 @@ xfs_ail_calc_push_target(return target_lsn; } +static void +xfsaild_process_logitem ( +struct xfs_ail *ailp, +struct xfs_log_item *lip, +int *stuck, +int *flushing) +{ +struct xfs_mount *mp = ailp->ail_log->l_mp; +uint type = lip->li_type; +unsigned long flags = lip->li_flags; +xfs_lsn_t item_lsn = lip->li_lsn; +int lock_result; + +/* +* Note that iop_push may unlock and reacquire the AIL lock. We +* rely on the AIL cursor implementation to be able to deal with +* the dropped lock. +* +* The log item may have been freed by the push, so it must not +* be accessed or dereferenced below this line. +*/ + lock_result =xfsaild_push_item (ailp, lip); +switch (lock_result) { +case XFS_ITEM_SUCCESS : +XFS_STATS_INC (mp, xs_push_ail_success); +trace_xfs_ail_push (ailp, type, flags, item_lsn); + + ailp->ail_last_pushed_lsn = item_lsn; +break ; + +case XFS_ITEM_FLUSHING : +/* +* The item or its backing buffer is already being +* flushed. The typical reason for that is that an +* inode buffer is locked because we already pushed the +* updates to it as part of inode clustering. +* +* We do not want to stop flushing just because lots +* of items are already being flushed, but we need to +* re-try the flushing relatively soon if most of the +* AIL is being flushed. +*/ +XFS_STATS_INC (mp, xs_push_ail_flushing); +trace_xfs_ail_flushing (ailp, type, flags, item_lsn); + + (*flushing)++; + ailp->ail_last_pushed_lsn = item_lsn; +break ; + +case XFS_ITEM_PINNED : +XFS_STATS_INC (mp, xs_push_ail_pinned); +trace_xfs_ail_pinned (ailp, type, flags, item_lsn); + + (*stuck)++; + ailp->ail_log_flush++; +break ; +case XFS_ITEM_LOCKED : +XFS_STATS_INC (mp, xs_push_ail_locked); +trace_xfs_ail_locked (ailp, type, flags, item_lsn); + + (*stuck)++; +break ; +default : +ASSERT (0 ); +break ; + } +} +static long xfsaild_push (struct xfs_ail *ailp)
@@ -511,68 +579,11 @@ xfsaild_push( lsn = lip->li_lsn;while ((XFS_LSN_CMP (lip->li_lsn, ailp->ail_target) <=0 )) { -int lock_result; -uint type = lip->li_type; -unsigned long flags = lip->li_flags; -xfs_lsn_t item_lsn = lip->li_lsn;if (test_bit (XFS_LI_FLUSHING , &lip->li_flags)) goto next_item; -/* -* Note that iop_push may unlock and reacquire the AIL lock. We -* rely on the AIL cursor implementation to be able to deal with -* the dropped lock. -* -* The log item may have been freed by the push, so it must not -* be accessed or dereferenced below this line. -*/ - lock_result =xfsaild_push_item (ailp, lip); -switch (lock_result) { -case XFS_ITEM_SUCCESS : -XFS_STATS_INC (mp, xs_push_ail_success); -trace_xfs_ail_push (ailp, type, flags, item_lsn); - - ailp->ail_last_pushed_lsn = item_lsn; -break ; - -case XFS_ITEM_FLUSHING : -/* -* The item or its backing buffer is already being -* flushed. The typical reason for that is that an -* inode buffer is locked because we already pushed the -* updates to it as part of inode clustering. -* -* We do not want to stop flushing just because lots -* of items are already being flushed, but we need to -* re-try the flushing relatively soon if most of the -* AIL is being flushed. -*/ -XFS_STATS_INC (mp, xs_push_ail_flushing); -trace_xfs_ail_flushing (ailp, type, flags, item_lsn); - - flushing++; - ailp->ail_last_pushed_lsn = item_lsn; -break ; - -case XFS_ITEM_PINNED : -XFS_STATS_INC (mp, xs_push_ail_pinned); -trace_xfs_ail_pinned (ailp, type, flags, item_lsn); - - stuck++; - ailp->ail_log_flush++; -break ; -case XFS_ITEM_LOCKED : -XFS_STATS_INC (mp, xs_push_ail_locked); -trace_xfs_ail_locked (ailp, type, flags, item_lsn); - - stuck++; -break ; -default : -ASSERT (0 ); -break ; - } - +xfsaild_process_logitem (ailp, lip, &stuck, &flushing); count++;/*
Signature · 8 lines
2.50.1 Amazon Web Services EMEA SARL, 38 avenue John F. Kennedy, L-1855 Luxembourg, R.C.S. Luxembourg B186284 Amazon Web Services EMEA SARL, Irish Branch, One Burlington Plaza, Burlington Road, Dublin 4, Ireland, branch registration number 908705
Re: [PATCH v4 4/4] xfs: refactor xfsaild_push loop into helper
Quoted 6 lines · Yuto Ohnuki
Factor the loop body of
xfsaild_push()into a separatexfsaild_process_logitem()helper to improve readability.This is a pure code movement with no functional change.
Signed-off-by Yuto Ohnuki <[email protected]>
Neat hoist!
--D
Quoted 166 lines
--- fs/xfs/xfs_trans_ail.c | 127 ++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 58 deletions(-)fs/xfs/xfs_trans_ail.c +69
-58@@ -464,6 +464,74 @@ xfs_ail_calc_push_target(return target_lsn; } +static void +xfsaild_process_logitem ( +struct xfs_ail *ailp, +struct xfs_log_item *lip, +int *stuck, +int *flushing) +{ +struct xfs_mount *mp = ailp->ail_log->l_mp; +uint type = lip->li_type; +unsigned long flags = lip->li_flags; +xfs_lsn_t item_lsn = lip->li_lsn; +int lock_result; + +/* +* Note that iop_push may unlock and reacquire the AIL lock. We +* rely on the AIL cursor implementation to be able to deal with +* the dropped lock. +* +* The log item may have been freed by the push, so it must not +* be accessed or dereferenced below this line. +*/ + lock_result =xfsaild_push_item (ailp, lip); +switch (lock_result) { +case XFS_ITEM_SUCCESS : +XFS_STATS_INC (mp, xs_push_ail_success); +trace_xfs_ail_push (ailp, type, flags, item_lsn); + + ailp->ail_last_pushed_lsn = item_lsn; +break ; + +case XFS_ITEM_FLUSHING : +/* +* The item or its backing buffer is already being +* flushed. The typical reason for that is that an +* inode buffer is locked because we already pushed the +* updates to it as part of inode clustering. +* +* We do not want to stop flushing just because lots +* of items are already being flushed, but we need to +* re-try the flushing relatively soon if most of the +* AIL is being flushed. +*/ +XFS_STATS_INC (mp, xs_push_ail_flushing); +trace_xfs_ail_flushing (ailp, type, flags, item_lsn); + + (*flushing)++; + ailp->ail_last_pushed_lsn = item_lsn; +break ; + +case XFS_ITEM_PINNED : +XFS_STATS_INC (mp, xs_push_ail_pinned); +trace_xfs_ail_pinned (ailp, type, flags, item_lsn); + + (*stuck)++; + ailp->ail_log_flush++; +break ; +case XFS_ITEM_LOCKED : +XFS_STATS_INC (mp, xs_push_ail_locked); +trace_xfs_ail_locked (ailp, type, flags, item_lsn); + + (*stuck)++; +break ; +default : +ASSERT (0 ); +break ; + } +} +static long xfsaild_push (struct xfs_ail *ailp)@@ -511,68 +579,11 @@ xfsaild_push( lsn = lip->li_lsn;while ((XFS_LSN_CMP (lip->li_lsn, ailp->ail_target) <=0 )) { -int lock_result; -uint type = lip->li_type; -unsigned long flags = lip->li_flags; -xfs_lsn_t item_lsn = lip->li_lsn;if (test_bit (XFS_LI_FLUSHING , &lip->li_flags)) goto next_item; -/* -* Note that iop_push may unlock and reacquire the AIL lock. We -* rely on the AIL cursor implementation to be able to deal with -* the dropped lock. -* -* The log item may have been freed by the push, so it must not -* be accessed or dereferenced below this line. -*/ - lock_result =xfsaild_push_item (ailp, lip); -switch (lock_result) { -case XFS_ITEM_SUCCESS : -XFS_STATS_INC (mp, xs_push_ail_success); -trace_xfs_ail_push (ailp, type, flags, item_lsn); - - ailp->ail_last_pushed_lsn = item_lsn; -break ; - -case XFS_ITEM_FLUSHING : -/* -* The item or its backing buffer is already being -* flushed. The typical reason for that is that an -* inode buffer is locked because we already pushed the -* updates to it as part of inode clustering. -* -* We do not want to stop flushing just because lots -* of items are already being flushed, but we need to -* re-try the flushing relatively soon if most of the -* AIL is being flushed. -*/ -XFS_STATS_INC (mp, xs_push_ail_flushing); -trace_xfs_ail_flushing (ailp, type, flags, item_lsn); - - flushing++; - ailp->ail_last_pushed_lsn = item_lsn; -break ; - -case XFS_ITEM_PINNED : -XFS_STATS_INC (mp, xs_push_ail_pinned); -trace_xfs_ail_pinned (ailp, type, flags, item_lsn); - - stuck++; - ailp->ail_log_flush++; -break ; -case XFS_ITEM_LOCKED : -XFS_STATS_INC (mp, xs_push_ail_locked); -trace_xfs_ail_locked (ailp, type, flags, item_lsn); - - stuck++; -break ; -default : -ASSERT (0 ); -break ; - } - +xfsaild_process_logitem (ailp, lip, &stuck, &flushing); count++;/* Signature · 8 lines
2.50.1 Amazon Web Services EMEA SARL, 38 avenue John F. Kennedy, L-1855 Luxembourg, R.C.S. Luxembourg B186284 Amazon Web Services EMEA SARL, Irish Branch, One Burlington Plaza, Burlington Road, Dublin 4, Ireland, branch registration number 908705
Quoted 10 lines · Yuto Ohnuki
When a filesystem is shut down, background inode reclaim and the xfsaild can race to abort and free dirty inodes. Since commit
90c60e164012("xfs:xfs_iflush()is no longer necessary"),xfs_inode_item_push()no longer holdsILOCK_SHAREDwhile flushing, removing the protection that prevented the inode from being reclaimed during the flush.This results in use-after-free when dereferencing log items after
iop_push()returns, or when reacquiring the AIL lock vialip->li_ailp.[...]
Applied to for-next, thanks!
[1/4] xfs: stop reclaim before pushing AIL during unmount
commit: 4f24a767e3d64a5f58c595b5c29b6063a201f1e3
[2/4] xfs: avoid dereferencing log items after push callbacks
commit: 79ef34ec0554ec04bdbafafbc9836423734e1bd6
[3/4] xfs: save ailp before dropping the AIL lock in push callbacks
commit: 394d70b86fae9fe865e7e6d9540b7696f73aa9b6
[4/4] xfs: refactor xfsaild_push loop into helper
commit: 7cac60947335f8d88a6390814840590a61134484Best regards,
Signature · 1 line
Carlos Maiolino <[email protected]>