xfs: shutdown is a racy mess
v3 cover xfs: shutdown is a racy mess
With the recent log problems we've uncovered, it's clear that the way we shut down filesystems and the log is a chaotic mess. We can have multiple filesystem shutdown executions being in progress at once, all competing to run shutdown processing and emit log messages saying the filesystem has been shut down and why. Further, shutdown changes the log state and runs log IO completion callbacks without any co-ordination with ongoing log operations.
This results in shutdowns running unpredictably, running multiple times, racing with the iclog state machine transitions and exposing us to use-after-free situations and unexpected state changes within the log itself.
This patch series tries to address the chaotic nature of shutdowns by making shutdown execution consistent and predictable. This is achieved by:
- making the mount shutdown state transistion atomic and not dependent on log state.
- making operational log state transitions atomic
- making the log shutdown check be based entirely on the operational XLOG_IO_ERROR log state rather than a combination of log flags and iclog XLOG_STATE_IOERROR checks.
- Getting rid of XLOG_STATE_IOERROR means shutdown doesn't perturb iclog state in the middle of operations that are expecting iclogs to be in specific state(s).
- shutdown doesn't process iclogs that are actively referenced. This avoids use-after-free situations where shutdown runs callbacks and frees objects that own the reference to the iclog and are still in use by the iclog reference owner.
- Run shutdown processing when the last active reference to an iclog goes away. This guarantees that shutdown processing occurs on all iclogs, but it only occurs when it is safe to do so.
- acknowledge that log state is not consistent once shutdown has been entered and so don't try to apply consistency checking during a shutdown...
At the end of this patch series, shutdown runs once and once only at the first trigger, iclog state is not modified by shutdown, and iclog callbacks and wakeups are not processed until all active references to the iclog(s) are dropped. Hence we now have deterministic shutdown behaviour for both the mount and the log and a consistent iclog lifecycle framework that we can build more complex functionality on top of safely.
Version 3:
- rebase on 5.14-rc4 + for-next @ 130916145229
- Fixed typos in commit messages
Version 2:
- https://lore.kernel.org/linux-xfs/[email protected]/
- rebase on 5.14-rc1
- added comment about XFS_FORCED_SHUTDOWN -> xlog_is_shutdown in commit message.
- fix spurious semi-colon at end of for loop.
- fixed typos in commit messages
- undid the do {} while -> for {} conversion in xlog_state_do_callbacks()
- removed spurious blank lines in xfs_do_force_shutdown()
- added comment to commit description explaining the unconditional stack dump on shutdown if the error level is high enough.
- added comment about iclog IO completion avoiding shutdown races with referenced iclogs that haven't yet been submitted to commit description.
- cleaned up xlog_state_release_iclog() structure for better readability.
- cleaned up xlog_space_left() structure for better readability.
Version 1:
- https://lore.kernel.org/linux-xfs/[email protected]/
PATCH v3 1/9 xfs: convert XLOG_FORCED_SHUTDOWN() to xlog_is_shutdown()
From: Dave Chinner <[email protected]>
Make it less shouty and a static inline before adding more calls through the log code.
Also convert internal log code that uses XFS_FORCED_SHUTDOWN(mount) to use xlog_is_shutdown(log) as well.
--- fs/xfs/xfs_log.c | 32 ++++++++++++++++---------------- fs/xfs/xfs_log_cil.c | 10 +++++----- fs/xfs/xfs_log_priv.h | 7 +++++-- fs/xfs/xfs_log_recover.c | 9 +++------ fs/xfs/xfs_trans.c | 2 +- 5 files changed, 30 insertions(+), 30 deletions(-)
fs/xfs/xfs_log.c +16 -16
@@ -246,7 +246,7 @@ xlog_grant_head_wait(list_add_tail (&tic->t_queue, &head->waiters); do { - if (XLOG_FORCED_SHUTDOWN(log)) + if (xlog_is_shutdown(log)) goto shutdown; xlog_grant_push_ail(log, need_bytes);
@@ -260,7 +260,7 @@ xlog_grant_head_wait(trace_xfs_log_grant_wake (log, tic);spin_lock (&head->lock); -if (XLOG_FORCED_SHUTDOWN (log)) +if (xlog_is_shutdown (log)) goto shutdown; }while (xlog_space_left (log, &head->grant) < need_bytes);
@@ -365,7 +365,7 @@ xfs_log_writable(return false;if (xfs_readonly_buftarg (mp->m_log->l_targ))return false; -if (XFS_FORCED_SHUTDOWN (mp)) +if (xlog_is_shutdown (mp->m_log))return false;return true; }
@@ -382,7 +382,7 @@ xfs_log_regrant(int need_bytes;int error =0 ; -if (XLOG_FORCED_SHUTDOWN (log)) +if (xlog_is_shutdown (log))return -EIO ;XFS_STATS_INC (mp, xs_try_logspace);
@@ -450,7 +450,7 @@ xfs_log_reserve(ASSERT (client ==XFS_TRANSACTION || client ==XFS_LOG ); -if (XLOG_FORCED_SHUTDOWN (log)) +if (xlog_is_shutdown (log))return -EIO ;XFS_STATS_INC (mp, xs_try_logspace);
@@ -830,7 +830,7 @@ xlog_wait_on_iclog(struct xlog *log = iclog->ic_log;trace_xlog_iclog_wait_on (iclog, _RET_IP_); -if (!XLOG_FORCED_SHUTDOWN (log) && +if (!xlog_is_shutdown (log) && iclog->ic_state !=XLOG_STATE_ACTIVE && iclog->ic_state !=XLOG_STATE_DIRTY ) {XFS_STATS_INC (log->l_mp, xs_log_force_sleep);
@@ -839,7 +839,7 @@ xlog_wait_on_iclog(spin_unlock (&log->l_icloglock); } -if (XLOG_FORCED_SHUTDOWN (log)) +if (xlog_is_shutdown (log))return -EIO ;return 0 ; }
@@ -940,7 +940,7 @@ xfs_log_unmount_write(xfs_log_force (mp,XFS_LOG_SYNC ); -if (XLOG_FORCED_SHUTDOWN (log)) +if (xlog_is_shutdown (log))return ;/*
@@ -1063,7 +1063,7 @@ xfs_log_space_wake(struct xlog *log = mp->m_log;int free_bytes; -if (XLOG_FORCED_SHUTDOWN (log)) +if (xlog_is_shutdown (log))return ;if (!list_empty_careful (&log->l_write_head.waiters)) {
@@ -1154,7 +1154,7 @@ xfs_log_cover(ASSERT ((xlog_cil_empty (mp->m_log) &&xlog_iclogs_empty (mp->m_log) && !xfs_ail_min_lsn (mp->m_log->l_ailp)) || -XFS_FORCED_SHUTDOWN (mp)); +xlog_is_shutdown (mp->m_log));if (!xfs_log_writable (mp))return 0 ;
@@ -1614,7 +1614,7 @@ xlog_commit_record( };int error; -if (XLOG_FORCED_SHUTDOWN (log)) +if (xlog_is_shutdown (log))return -EIO ; error =xlog_write (log, &vec, ticket, lsn, iclog,XLOG_COMMIT_TRANS );
@@ -1695,7 +1695,7 @@ xlog_grant_push_ail(xfs_lsn_t threshold_lsn; threshold_lsn =xlog_grant_push_threshold (log, need_bytes); -if (threshold_lsn ==NULLCOMMITLSN ||XLOG_FORCED_SHUTDOWN (log)) +if (threshold_lsn ==NULLCOMMITLSN ||xlog_is_shutdown (log))return ;/*
@@ -2886,7 +2886,7 @@ xlog_state_do_callback( cycled_icloglock = true;spin_lock (&log->l_icloglock); -if (XLOG_FORCED_SHUTDOWN (log)) +if (xlog_is_shutdown (log))wake_up_all (&iclog->ic_force_wait);else xlog_state_clean_iclog (log, iclog);
@@ -2938,7 +2938,7 @@ xlog_state_done_syncing(* split log writes, on the second, we shut down the file system and * no iclogs should ever be attempted to be written to disk again. */ -if (!XLOG_FORCED_SHUTDOWN (log)) { +if (!xlog_is_shutdown (log)) {ASSERT (iclog->ic_state ==XLOG_STATE_SYNCING ); iclog->ic_state =XLOG_STATE_DONE_SYNC ; }
@@ -2986,7 +2986,7 @@ xlog_state_get_iclog_space( restart:spin_lock (&log->l_icloglock); -if (XLOG_FORCED_SHUTDOWN (log)) { +if (xlog_is_shutdown (log)) {spin_unlock (&log->l_icloglock);return -EIO ; }
@@ -3877,7 +3877,7 @@ xfs_log_force_umount(* No need to get locks for this. */ if (logerror && log->l_iclog->ic_state ==XLOG_STATE_IOERROR ) { -ASSERT (XLOG_FORCED_SHUTDOWN (log)); +ASSERT (xlog_is_shutdown (log));return 1 ; }
fs/xfs/xfs_log_cil.c +5 -5
@@ -584,7 +584,7 @@ xlog_cil_committed( struct xfs_cil_ctx *ctx) {struct xfs_mount *mp = ctx->cil->xc_log->l_mp; -bool abort =XLOG_FORCED_SHUTDOWN (ctx->cil->xc_log); +bool abort =xlog_is_shutdown (ctx->cil->xc_log);/* * If the I/O failed, we're aborting the commit and already shutdown.
@@ -862,7 +862,7 @@ xlog_cil_push_work(* shutdown, but then went back to sleep once already in the * shutdown state. */ -if (XLOG_FORCED_SHUTDOWN (log)) { +if (xlog_is_shutdown (log)) {spin_unlock (&cil->xc_push_lock); goto out_abort_free_ticket; }
@@ -971,7 +971,7 @@ xlog_cil_push_work( out_abort_free_ticket:xfs_log_ticket_ungrant (log, tic); out_abort: -ASSERT (XLOG_FORCED_SHUTDOWN (log)); +ASSERT (xlog_is_shutdown (log));xlog_cil_committed (ctx); }
@@ -1124,7 +1124,7 @@ xlog_cil_commit(xlog_cil_insert_items (log, tp); -if (regrant && !XLOG_FORCED_SHUTDOWN (log)) +if (regrant && !xlog_is_shutdown (log))xfs_log_ticket_regrant (log, tp->t_ticket);else xfs_log_ticket_ungrant (log, tp->t_ticket);
@@ -1197,7 +1197,7 @@ xlog_cil_force_seq(* shutdown, but then went back to sleep once already in the * shutdown state. */ -if (XLOG_FORCED_SHUTDOWN (log)) +if (xlog_is_shutdown (log)) goto out_shutdown;if (ctx->sequence > sequence)continue ;
fs/xfs/xfs_log_priv.h +5 -2
@@ -464,8 +464,11 @@ struct xlog {#define XLOG_BUF_CANCEL_BUCKET (log, blkno) \ ((log)->l_buf_cancel_table + ((uint64_t)blkno % XLOG_BC_TABLE_SIZE)) -#define XLOG_FORCED_SHUTDOWN (log) \ - (unlikely((log)->l_flags & XLOG_IO_ERROR)) +static inline bool +xlog_is_shutdown (struct xlog *log) +{ +return (log->l_flags &XLOG_IO_ERROR ); +}/* common routines */ extern int
fs/xfs/xfs_log_recover.c +3 -6
@@ -146,7 +146,7 @@ xlog_do_io( error =xfs_rw_bdev (log->l_targ->bt_bdev, log->l_logBBstart + blk_no,BBTOB (nbblks), data, op); -if (error && !XFS_FORCED_SHUTDOWN (log->l_mp)) { +if (error && !xlog_is_shutdown (log)) { xfs_alert(log->l_mp, "log recovery %s I/O error at daddr 0x%llx len %d error %d", op == REQ_OP_WRITE ? "write" : "read",
@@ -3313,10 +3313,7 @@ xlog_do_recover(if (error)return error; -/* -* If IO errors happened during recovery, bail out. -*/ -if (XFS_FORCED_SHUTDOWN (mp)) +if (xlog_is_shutdown (log))return -EIO ;/*
@@ -3338,7 +3335,7 @@ xlog_do_recover(xfs_buf_hold (bp); error =_xfs_buf_read (bp,XBF_READ );if (error) { -if (!XFS_FORCED_SHUTDOWN (mp)) { +if (!xlog_is_shutdown (log)) {xfs_buf_ioerror_alert (bp, __this_address);ASSERT (0 ); }
fs/xfs/xfs_trans.c +1 -1
@@ -905,7 +905,7 @@ __xfs_trans_commit(*/ xfs_trans_unreserve_and_mod_dquots (tp);if (tp->t_ticket) { -if (regrant && !XLOG_FORCED_SHUTDOWN (mp->m_log)) +if (regrant && !xlog_is_shutdown (mp->m_log))xfs_log_ticket_regrant (mp->m_log, tp->t_ticket);else xfs_log_ticket_ungrant (mp->m_log, tp->t_ticket);
Signature · 1 line
2.31.1
PATCH v3 2/9 xfs: XLOG_STATE_IOERROR must die
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
We don't need an iclog state field to tell us the log has been shut down. We can just check the xlog_is_shutdown() instead. The avoids the need to have shutdown overwrite the current iclog state while being active used by the log code and so having to ensure that every iclog state check handles XLOG_STATE_IOERROR appropriately.
--- fs/xfs/xfs_log.c | 108 ++++++++++++------------------------------ fs/xfs/xfs_log_cil.c | 2 +- fs/xfs/xfs_log_priv.h | 4 +- fs/xfs/xfs_trace.h | 1 - 4 files changed, 32 insertions(+), 83 deletions(-)
fs/xfs/xfs_log.c +30 -78
@@ -523,7 +523,7 @@ xlog_state_release_iclog(lockdep_assert_held (&log->l_icloglock);trace_xlog_iclog_release (iclog, _RET_IP_); -if (iclog->ic_state ==XLOG_STATE_IOERROR ) +if (xlog_is_shutdown (log))return -EIO ;/*
@@ -893,7 +893,7 @@ xlog_unmount_write( error =xlog_write_unmount_record (log, tic);/* * At this point, we're umounting anyway, so there's no point in -* transitioning log state to IOERROR. Just continue... +* transitioning log state to shutdown. Just continue... */ out_err:if (error)
@@ -1837,7 +1837,7 @@ xlog_write_iclog(* across the log IO to archieve that. */ down (&iclog->ic_sema); -if (unlikely (iclog->ic_state ==XLOG_STATE_IOERROR )) { +if (xlog_is_shutdown (log)) {/* * It would seem logical to return EIO here, but we rely on * the log state machine to propagate I/O errors instead of
@@ -2376,7 +2376,7 @@ xlog_write_copy_finish(xlog_state_switch_iclogs (log, iclog,0 ); else ASSERT(iclog->ic_state == XLOG_STATE_WANT_SYNC || - iclog->ic_state == XLOG_STATE_IOERROR); +xlog_is_shutdown (log ));if (!commit_iclog) goto release_iclog;spin_unlock (&log->l_icloglock);
@@ -2793,8 +2793,7 @@ xlog_state_set_callback(static bool xlog_state_iodone_process_iclog (struct xlog *log, -struct xlog_in_core *iclog, -bool *ioerror) +struct xlog_in_core *iclog) {xfs_lsn_t lowest_lsn;xfs_lsn_t header_lsn;
@@ -2806,15 +2805,6 @@ xlog_state_iodone_process_iclog(* Skip all iclogs in the ACTIVE & DIRTY states: */ return false; -case XLOG_STATE_IOERROR : -/* -* Between marking a filesystem SHUTDOWN and stopping the log, -* we do flush all iclogs to disk (if there wasn't a log I/O -* error). So, we do want things to go smoothly in case of just -* a SHUTDOWN w/o a LOG_IO_ERROR. -*/ - *ioerror = true; -return false;case XLOG_STATE_DONE_SYNC :/* * Now that we have an iclog that is in the DONE_SYNC state, do
@@ -2845,7 +2835,6 @@ xlog_state_do_callback(struct xlog_in_core *iclog;struct xlog_in_core *first_iclog;bool cycled_icloglock; -bool ioerror;int flushcnt =0 ;int repeats =0 ;
@@ -2859,23 +2848,20 @@ xlog_state_do_callback(* Keep looping through iclogs until one full pass is made * without running any callbacks. */ - first_iclog = log->l_iclog; - iclog = log->l_iclog; cycled_icloglock = false; - ioerror = false; - repeats++; + first_iclog = log->l_iclog; + iclog = first_iclog; do { LIST_HEAD(cb_list); - if (xlog_state_iodone_process_iclog(log, iclog, - &ioerror)) - break; - - if (iclog->ic_state != XLOG_STATE_CALLBACK && - iclog->ic_state != XLOG_STATE_IOERROR) { - iclog = iclog->ic_next; - continue; + if (!xlog_is_shutdown(log)) { + if (xlog_state_iodone_process_iclog(log, iclog)) + break; + if (iclog->ic_state != XLOG_STATE_CALLBACK) { + iclog = iclog->ic_next; + continue; + } } list_splice_init(&iclog->ic_callbacks, &cb_list); spin_unlock(&log->l_icloglock);
@@ -2891,19 +2877,19 @@ xlog_state_do_callback(else xlog_state_clean_iclog (log, iclog); iclog = iclog->ic_next; - }while (first_iclog != iclog); + }while (iclog != first_iclog); -if (repeats >5000 ) { +if (++repeats >5000 ) { flushcnt += repeats; repeats =0 ;xfs_warn (log->l_mp,"%s: possible infinite loop (%d iterations)" , __func__, flushcnt); } - }while (!ioerror && cycled_icloglock); + }while (!xlog_is_shutdown (log) && cycled_icloglock);if (log->l_iclog->ic_state ==XLOG_STATE_ACTIVE || - log->l_iclog->ic_state ==XLOG_STATE_IOERROR ) +xlog_is_shutdown (log))wake_up_all (&log->l_flush_wait);spin_unlock (&log->l_icloglock);
@@ -2913,13 +2899,6 @@ xlog_state_do_callback(/* * Finish transitioning this iclog to the dirty state. * -* Make sure that we completely execute this routine only when this is -* the last call to the iclog. There is a good chance that iclog flushes, -* when we reach the end of the physical log, get turned into 2 separate -* calls to bwrite. Hence, one iclog flush could generate two calls to this -* routine. By using the reference count bwritecnt, we guarantee that only -* the second completion goes through. -* * Callbacks could take time, so they are done outside the scope of the * global state machine log lock. */
@@ -3279,10 +3258,10 @@ xfs_log_force(xlog_cil_force (log);spin_lock (&log->l_icloglock); - iclog = log->l_iclog; -if (iclog->ic_state ==XLOG_STATE_IOERROR ) +if (xlog_is_shutdown (log)) goto out_error; + iclog = log->l_iclog;trace_xlog_iclog_force (iclog, _RET_IP_); if (iclog->ic_state == XLOG_STATE_DIRTY ||
@@ -3348,10 +3327,10 @@ xlog_force_lsn(bool completed;spin_lock (&log->l_icloglock); - iclog = log->l_iclog; -if (iclog->ic_state ==XLOG_STATE_IOERROR ) +if (xlog_is_shutdown (log)) goto out_error; + iclog = log->l_iclog;while (be64_to_cpu (iclog->ic_header.h_lsn) != lsn) {trace_xlog_iclog_force_lsn (iclog, _RET_IP_); iclog = iclog->ic_next;
@@ -3806,34 +3785,6 @@ xlog_verify_iclog( }#endif -/* -* Mark all iclogs IOERROR. l_icloglock is held by the caller. -*/ -STATIC int -xlog_state_ioerror ( -struct xlog *log) -{ -xlog_in_core_t *iclog, *ic; - - iclog = log->l_iclog; -if (iclog->ic_state !=XLOG_STATE_IOERROR ) { -/* -* Mark all the incore logs IOERROR. -* From now on, no log flushes will result. -*/ - ic = iclog; -do { - ic->ic_state =XLOG_STATE_IOERROR ; - ic = ic->ic_next; - }while (ic != iclog); -return 0 ; - } -/* -* Return non-zero, if state transition has already happened. -*/ -return 1 ; -} -/* * This is called from xfs_force_shutdown, when we're forcibly * shutting down the filesystem, typically because of an IO error.
@@ -3849,6 +3800,8 @@ xlog_state_ioerror(* Note: for the !logerror case we need to flush the regions held in memory out * to disk first. This needs to be done before the log is marked as shutdown, * otherwise the iclog writes will fail. +* +* Return non-zero if log shutdown transition had already happened. */ int xfs_log_force_umount(
@@ -3856,7 +3809,7 @@ xfs_log_force_umount( int logerror) {struct xlog *log; -int retval; +int retval =0 ; log = mp->m_log;
@@ -3876,10 +3829,8 @@ xfs_log_force_umount(* Somebody could've already done the hard work for us. * No need to get locks for this. */ -if (logerror && log->l_iclog->ic_state ==XLOG_STATE_IOERROR ) { -ASSERT (xlog_is_shutdown (log)); +if (logerror &&xlog_is_shutdown (log))return 1 ; - }/* * Flush all the completed transactions to disk before marking the log
@@ -3904,8 +3855,10 @@ xfs_log_force_umount(* Mark the log and the iclogs with IO error flags to prevent any * further log IO from being issued or completed. */ - log->l_flags |=XLOG_IO_ERROR ; - retval =xlog_state_ioerror (log); +if (!(log->l_flags &XLOG_IO_ERROR )) { + log->l_flags |=XLOG_IO_ERROR ; + retval =1 ; + }spin_unlock (&log->l_icloglock);/*
@@ -3929,7 +3882,6 @@ xfs_log_force_umount(spin_unlock (&log->l_cilp->xc_push_lock);xlog_state_do_callback (log); -/* return non-zero if log IOERROR transition had already happened */ return retval; }
fs/xfs/xfs_log_cil.c +1 -1
@@ -898,7 +898,7 @@ xlog_cil_push_work(* callbacks and dropped the icloglock. */ spin_lock (&log->l_icloglock); -if (commit_iclog->ic_state ==XLOG_STATE_IOERROR ) { +if (xlog_is_shutdown (log)) {spin_unlock (&log->l_icloglock); goto out_abort; }
fs/xfs/xfs_log_priv.h +1 -3
@@ -47,7 +47,6 @@ enum xlog_iclog_state {XLOG_STATE_DONE_SYNC ,/* Done syncing to disk */ XLOG_STATE_CALLBACK ,/* Callback functions now */ XLOG_STATE_DIRTY, /* Dirty IC log, not ready for ACTIVE status */ - XLOG_STATE_IOERROR, /* IO error happened in sync'ing log */ };#define XLOG_STATE_STRINGS \
@@ -56,8 +55,7 @@ enum xlog_iclog_state { { XLOG_STATE_SYNCING, "XLOG_STATE_SYNCING" }, \ { XLOG_STATE_DONE_SYNC, "XLOG_STATE_DONE_SYNC" }, \ { XLOG_STATE_CALLBACK, "XLOG_STATE_CALLBACK" }, \ - { XLOG_STATE_DIRTY, "XLOG_STATE_DIRTY" }, \ - { XLOG_STATE_IOERROR, "XLOG_STATE_IOERROR" } + { XLOG_STATE_DIRTY, "XLOG_STATE_DIRTY" }/* * In core log flags
fs/xfs/xfs_trace.h +0 -1
@@ -4017,7 +4017,6 @@ TRACE_DEFINE_ENUM(XLOG_STATE_SYNCING);TRACE_DEFINE_ENUM (XLOG_STATE_DONE_SYNC );TRACE_DEFINE_ENUM (XLOG_STATE_CALLBACK );TRACE_DEFINE_ENUM (XLOG_STATE_DIRTY ); -TRACE_DEFINE_ENUM (XLOG_STATE_IOERROR ); DECLARE_EVENT_CLASS(xlog_iclog_class, TP_PROTO(struct xlog_in_core *iclog, unsigned long caller_ip),
Signature · 1 line
2.31.1
PATCH v3 3/9 xfs: move recovery needed state updates to xfs_log_mount_finish
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
xfs_log_mount_finish() needs to know if recovery is needed or not to make decisions on whether to flush the log and AIL. Move the handling of the NEED_RECOVERY state out to this function rather than needing a temporary variable to store this state over the call to xlog_recover_finish().
--- fs/xfs/xfs_log.c | 24 ++++++---- fs/xfs/xfs_log_recover.c | 100 ++++++++++++++++----------------------- 2 files changed, 56 insertions(+), 68 deletions(-)
fs/xfs/xfs_log.c +16 -8
@@ -726,9 +726,9 @@ intxfs_log_mount_finish ( struct xfs_mount *mp) { -int error =0 ; -bool readonly = (mp->m_flags &XFS_MOUNT_RDONLY ); -bool recovered = mp->m_log->l_flags &XLOG_RECOVERY_NEEDED ; +struct xlog *log = mp->m_log; +bool readonly = (mp->m_flags &XFS_MOUNT_RDONLY ); +int error =0 ;if (mp->m_flags &XFS_MOUNT_NORECOVERY ) {ASSERT (mp->m_flags &XFS_MOUNT_RDONLY );
@@ -759,7 +759,8 @@ xfs_log_mount_finish(* mount failure occurs. */ mp->m_super->s_flags |=SB_ACTIVE ; - error =xlog_recover_finish (mp->m_log); +if (log->l_flags &XLOG_RECOVERY_NEEDED ) + error =xlog_recover_finish (log);if (!error)xfs_log_work_queue (mp); mp->m_super->s_flags &= ~SB_ACTIVE ;
@@ -774,17 +775,24 @@ xfs_log_mount_finish(* Don't push in the error case because the AIL may have pending intents * that aren't removed until recovery is cancelled. */ -if (!error && recovered) { -xfs_log_force (mp,XFS_LOG_SYNC ); -xfs_ail_push_all_sync (mp->m_ail); +if (log->l_flags &XLOG_RECOVERY_NEEDED ) { +if (!error) { +xfs_log_force (mp,XFS_LOG_SYNC ); +xfs_ail_push_all_sync (mp->m_ail); + } +xfs_notice (mp,"Ending recovery (logdev: %s)" , + mp->m_logname ? mp->m_logname :"internal" ); + }else { +xfs_info (mp,"Ending clean mount" ); }xfs_buftarg_drain (mp->m_ddev_targp); + log->l_flags &= ~XLOG_RECOVERY_NEEDED ;if (readonly) mp->m_flags |=XFS_MOUNT_RDONLY ;/* Make sure the log is dead if we're returning failure. */ -ASSERT (!error || (mp->m_log->l_flags &XLOG_IO_ERROR )); +ASSERT (!error ||xlog_is_shutdown (log));return error; }
fs/xfs/xfs_log_recover.c +40 -60
118 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v3 4/9 xfs: convert log flags to an operational state field
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
log->l_flags doesn't actually contain "flags" as such, it contains operational state information that can change at runtime. For the shutdown state, this at least should be an atomic bit because it is read without holding locks in many places and so using atomic bitops for the state field modifications makes sense.
This allows us to use things like test_and_set_bit() on state changes (e.g. setting XLOG_TAIL_WARN) to avoid races in setting the state when we aren't holding locks.
--- fs/xfs/xfs_log.c | 58 ++++++++++++++++------------------------ fs/xfs/xfs_log.h | 1 - fs/xfs/xfs_log_priv.h | 34 +++++++++++++++-------- fs/xfs/xfs_log_recover.c | 6 ++--- fs/xfs/xfs_super.c | 2 +- 5 files changed, 50 insertions(+), 51 deletions(-)
fs/xfs/xfs_log.c +23 -35
@@ -298,7 +298,7 @@ xlog_grant_head_check(int free_bytes;int error =0 ; -ASSERT (!(log->l_flags &XLOG_ACTIVE_RECOVERY )); +ASSERT (!xlog_in_recovery (log));/* * If there are other waiters on the queue then give them a chance at
@@ -580,6 +580,7 @@ xfs_log_mount( xfs_daddr_t blk_offset, int num_bblks) { +struct xlog *log;bool fatal =xfs_sb_version_hascrc (&mp->m_sb);int error =0 ;int min_logfsbs;
@@ -594,11 +595,12 @@ xfs_log_mount(ASSERT (mp->m_flags &XFS_MOUNT_RDONLY ); } - mp->m_log =xlog_alloc_log (mp, log_target, blk_offset, num_bblks); -if (IS_ERR (mp->m_log)) { - error =PTR_ERR (mp->m_log); + log =xlog_alloc_log (mp, log_target, blk_offset, num_bblks); +if (IS_ERR (log)) { + error =PTR_ERR (log); goto out; } + mp->m_log = log;/* * Validate the given log space and drop a critical message via syslog
@@ -663,7 +665,7 @@ xfs_log_mount(xfs_warn (mp,"AIL initialisation failed: error %d" , error); goto out_free_log; } - mp->m_log->l_ailp = mp->m_ail; + log->l_ailp = mp->m_ail;/* * skip log recovery on a norecovery mount. pretend it all
@@ -675,39 +677,39 @@ xfs_log_mount(if (readonly) mp->m_flags &= ~XFS_MOUNT_RDONLY ; - error =xlog_recover (mp->m_log); + error =xlog_recover (log);if (readonly) mp->m_flags |=XFS_MOUNT_RDONLY ;if (error) {xfs_warn (mp,"log mount/recovery failed: error %d" , error); -xlog_recover_cancel (mp->m_log); +xlog_recover_cancel (log); goto out_destroy_ail; } } - error =xfs_sysfs_init (&mp->m_log->l_kobj, &xfs_log_ktype, &mp->m_kobj, + error =xfs_sysfs_init (&log->l_kobj, &xfs_log_ktype, &mp->m_kobj,"log" );if (error) goto out_destroy_ail;/* Normal transactions can now occur */ - mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY ; +clear_bit (XLOG_ACTIVE_RECOVERY , &log->l_opstate);/* * Now the log has been fully initialised and we know were our * space grant counters are, we can initialise the permanent ticket * needed for delayed logging to work. */ -xlog_cil_init_post_recovery (mp->m_log); +xlog_cil_init_post_recovery (log);return 0 ; out_destroy_ail:xfs_trans_ail_destroy (mp); out_free_log: -xlog_dealloc_log (mp->m_log); +xlog_dealloc_log (log); out:return error; }
@@ -759,7 +761,7 @@ xfs_log_mount_finish(* mount failure occurs. */ mp->m_super->s_flags |=SB_ACTIVE ; -if (log->l_flags &XLOG_RECOVERY_NEEDED ) +if (xlog_recovery_needed (log)) error =xlog_recover_finish (log);if (!error)xfs_log_work_queue (mp);
@@ -775,7 +777,7 @@ xfs_log_mount_finish(* Don't push in the error case because the AIL may have pending intents * that aren't removed until recovery is cancelled. */ -if (log->l_flags &XLOG_RECOVERY_NEEDED ) { +if (xlog_recovery_needed (log)) {if (!error) {xfs_log_force (mp,XFS_LOG_SYNC );xfs_ail_push_all_sync (mp->m_ail);
@@ -787,7 +789,7 @@ xfs_log_mount_finish( }xfs_buftarg_drain (mp->m_ddev_targp); - log->l_flags &= ~XLOG_RECOVERY_NEEDED ; +clear_bit (XLOG_RECOVERY_NEEDED , &log->l_opstate);if (readonly) mp->m_flags |=XFS_MOUNT_RDONLY ;
@@ -1075,7 +1077,7 @@ xfs_log_space_wake(return ;if (!list_empty_careful (&log->l_write_head.waiters)) { -ASSERT (!(log->l_flags &XLOG_ACTIVE_RECOVERY )); +ASSERT (!xlog_in_recovery (log));spin_lock (&log->l_write_head.lock); free_bytes =xlog_space_left (log, &log->l_write_head.grant);
@@ -1084,7 +1086,7 @@ xfs_log_space_wake( }if (!list_empty_careful (&log->l_reserve_head.waiters)) { -ASSERT (!(log->l_flags &XLOG_ACTIVE_RECOVERY )); +ASSERT (!xlog_in_recovery (log));spin_lock (&log->l_reserve_head.lock); free_bytes =xlog_space_left (log, &log->l_reserve_head.grant);
@@ -1466,7 +1468,7 @@ xlog_alloc_log( log->l_logBBstart = blk_offset; log->l_logBBsize = num_bblks; log->l_covered_state =XLOG_STATE_COVER_IDLE ; - log->l_flags |=XLOG_ACTIVE_RECOVERY ; +set_bit (XLOG_ACTIVE_RECOVERY , &log->l_opstate);INIT_DELAYED_WORK (&log->l_work, xfs_log_worker); log->l_prev_block =-1 ;
@@ -3648,17 +3650,15 @@ xlog_verify_grant_tail(xlog_crack_atomic_lsn (&log->l_tail_lsn, &tail_cycle, &tail_blocks);if (tail_cycle != cycle) {if (cycle -1 != tail_cycle && - !(log->l_flags &XLOG_TAIL_WARN )) { + !test_and_set_bit (XLOG_TAIL_WARN , &log->l_opstate)) {xfs_alert_tag (log->l_mp,XFS_PTAG_LOGRES ,"%s: cycle - 1 != tail_cycle" , __func__); - log->l_flags |=XLOG_TAIL_WARN ; }if (space >BBTOB (tail_blocks) && - !(log->l_flags &XLOG_TAIL_WARN )) { + !test_and_set_bit (XLOG_TAIL_WARN , &log->l_opstate)) {xfs_alert_tag (log->l_mp,XFS_PTAG_LOGRES ,"%s: space > BBTOB(tail_blocks)" , __func__); - log->l_flags |=XLOG_TAIL_WARN ; } } }
@@ -3825,8 +3825,7 @@ xfs_log_force_umount(* If this happens during log recovery, don't worry about * locking; the log isn't open for business yet. */ -if (!log || - log->l_flags &XLOG_ACTIVE_RECOVERY ) { +if (!log ||xlog_in_recovery (log)) { mp->m_flags |=XFS_MOUNT_FS_SHUTDOWN ;if (mp->m_sb_bp) mp->m_sb_bp->b_flags |=XBF_DONE ;
@@ -3863,10 +3862,8 @@ xfs_log_force_umount(* Mark the log and the iclogs with IO error flags to prevent any * further log IO from being issued or completed. */ -if (!(log->l_flags &XLOG_IO_ERROR )) { - log->l_flags |=XLOG_IO_ERROR ; +if (!test_and_set_bit (XLOG_IO_ERROR , &log->l_opstate)) retval =1 ; - }spin_unlock (&log->l_icloglock);/*
@@ -3954,15 +3951,6 @@ xfs_log_check_lsn(return valid; } -bool -xfs_log_in_recovery ( -struct xfs_mount *mp) -{ -struct xlog *log = mp->m_log; - -return log->l_flags &XLOG_ACTIVE_RECOVERY ; -} -/* * Notify the log that we're about to start using a feature that is protected * by a log incompat feature flag. This will prevent log covering from
fs/xfs/xfs_log.h +0 -1
@@ -138,7 +138,6 @@ void xfs_log_work_queue(struct xfs_mount *mp);int xfs_log_quiesce (struct xfs_mount *mp);void xfs_log_clean (struct xfs_mount *mp);bool xfs_log_check_lsn (struct xfs_mount *,xfs_lsn_t ); -bool xfs_log_in_recovery (struct xfs_mount *);xfs_lsn_t xlog_grant_push_threshold (struct xlog *log,int need_bytes);
fs/xfs/xfs_log_priv.h +23 -11
@@ -11,15 +11,6 @@ struct xlog;struct xlog_ticket ;struct xfs_mount ; -/* -* Flags for log structure -*/ -#define XLOG_ACTIVE_RECOVERY 0x2/* in the middle of recovery */ -#define XLOG_RECOVERY_NEEDED 0x4/* log was recovered */ -#define XLOG_IO_ERROR 0x8/* log hit an I/O error, and being -shutdown */ -#define XLOG_TAIL_WARN 0x10/* log tail verify warning issued */ -/* * get client id from packed copy. *
@@ -405,7 +396,7 @@ struct xlog {struct xfs_buftarg *l_targ;/* buftarg of log */ struct workqueue_struct *l_ioend_workqueue;/* for I/O completions */ struct delayed_work l_work;/* background flush work */ -uint l_flags; +long l_opstate;/* operational state */ uint l_quotaoffs_flag;/* XFS_DQ_*, for QUOTAOFFs */ struct list_head *l_buf_cancel_table;int l_iclog_hsize;/* size of iclog header */
@@ -462,10 +453,31 @@ struct xlog {#define XLOG_BUF_CANCEL_BUCKET (log, blkno) \ ((log)->l_buf_cancel_table + ((uint64_t)blkno % XLOG_BC_TABLE_SIZE)) +/* +* Bits for operational state +*/ +#define XLOG_ACTIVE_RECOVERY 0/* in the middle of recovery */ +#define XLOG_RECOVERY_NEEDED 1/* log was recovered */ +#define XLOG_IO_ERROR 2/* log hit an I/O error, and being +shutdown */ +#define XLOG_TAIL_WARN 3/* log tail verify warning issued */ + +static inline bool +xlog_recovery_needed (struct xlog *log) +{ +return test_bit (XLOG_RECOVERY_NEEDED , &log->l_opstate); +} + +static inline bool +xlog_in_recovery (struct xlog *log) +{ +return test_bit (XLOG_ACTIVE_RECOVERY , &log->l_opstate); +} +static inline bool xlog_is_shutdown (struct xlog *log) { -return (log->l_flags &XLOG_IO_ERROR ); +return test_bit (XLOG_IO_ERROR , &log->l_opstate); }/* common routines */
fs/xfs/xfs_log_recover.c +3 -3
@@ -3359,7 +3359,7 @@ xlog_do_recover(xlog_recover_check_summary (log);/* Normal transactions can now occur */ - log->l_flags &= ~XLOG_ACTIVE_RECOVERY ; +clear_bit (XLOG_ACTIVE_RECOVERY , &log->l_opstate);return 0 ; }
@@ -3443,7 +3443,7 @@ xlog_recover( : "internal"); error =xlog_do_recover (log, head_blk, tail_blk); - log->l_flags |=XLOG_RECOVERY_NEEDED ; +set_bit (XLOG_RECOVERY_NEEDED , &log->l_opstate); }return error; }
@@ -3508,7 +3508,7 @@ voidxlog_recover_cancel ( struct xlog *log) { -if (log->l_flags &XLOG_RECOVERY_NEEDED ) +if (xlog_recovery_needed (log))xlog_recover_cancel_intents (log); }
fs/xfs/xfs_super.c +1 -1
@@ -710,7 +710,7 @@ xfs_fs_drop_inode(* that. See the comment for this inode flag. */ if (ip->i_flags &XFS_IRECOVERY ) { -ASSERT (ip->i_mount->m_log->l_flags &XLOG_RECOVERY_NEEDED ); +ASSERT (xlog_recovery_needed (ip->i_mount->m_log));return 0 ; }
Signature · 1 line
2.31.1
PATCH v3 5/9 xfs: make forced shutdown processing atomic
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
The running of a forced shutdown is a bit of a mess. It does racy checks for XFS_MOUNT_SHUTDOWN in xfs_do_force_shutdown(), then does more racy checks in xfs_log_force_unmount() before finally setting XFS_MOUNT_SHUTDOWN and XLOG_IO_ERROR under the log->icloglock.
Move the checking and setting of XFS_MOUNT_SHUTDOWN into xfs_do_force_shutdown() so we only process a shutdown once and once only. Serialise this with the mp->m_sb_lock spinlock so that the state change is atomic and won't race. Move all the mount specific shutdown state changes from xfs_log_force_unmount() to xfs_do_force_shutdown() so they are done atomically with setting XFS_MOUNT_SHUTDOWN.
Then get rid of the racy xlog_is_shutdown() check from xlog_force_shutdown(), and gate the log shutdown on the test_and_set_bit(XLOG_IO_ERROR) test under the icloglock. This means that the log is shutdown once and once only, and code that needs to prevent races with shutdown can do so by holding the icloglock and checking the return value of xlog_is_shutdown().
This results in a predictable shutdown execution process - we set the shutdown flags once and process the shutdown once rather than the current "as many concurrent shutdowns as can race to the flag setting" situation we have now.
Also, now that shutdown is atomic, alway emit a stack trace when the error level for the filesystem is high enough. This means that we always get a stack trace when trying to diagnose the cause of shutdowns in the field, rather than just for SHUTDOWN_CORRUPT_INCORE cases.
--- fs/xfs/xfs_fsops.c | 63 ++++++++++++++-------------- fs/xfs/xfs_log.c | 100 ++++++++++++++++++++------------------------- fs/xfs/xfs_log.h | 2 +- 3 files changed, 76 insertions(+), 89 deletions(-)
fs/xfs/xfs_fsops.c +30 -33
@@ -511,6 +511,11 @@ xfs_fs_goingdown(* consistent. We don't do an unmount here; just shutdown the shop, make sure * that absolutely nothing persistent happens to this filesystem after this * point. +* +* The shutdown state change is atomic, resulting in the first and only the +* first shutdown call processing the shutdown. This means we only shutdown the +* log once as it requires, and we don't spam the logs when multiple concurrent +* shutdowns race to set the shutdown flags. */ void xfs_do_force_shutdown(
@@ -519,48 +524,40 @@ xfs_do_force_shutdown(char *fname, int lnnum) { - bool logerror = flags & SHUTDOWN_LOG_IO_ERROR; + int tag; +const char *why; -/* -* No need to duplicate efforts. -*/ -if (XFS_FORCED_SHUTDOWN (mp) && !logerror) -return ; - -/* -* This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't -* queue up anybody new on the log reservations, and wakes up -* everybody who's sleeping on log reservations to tell them -* the bad news. -*/ -if (xfs_log_force_umount (mp, logerror)) -return ; - -if (flags &SHUTDOWN_FORCE_UMOUNT ) { -xfs_alert (mp, -"User initiated shutdown (0x%x) received. Shutting down filesystem" , - flags); +spin_lock (&mp->m_sb_lock); +if (XFS_FORCED_SHUTDOWN (mp)) { +spin_unlock (&mp->m_sb_lock);return ; } + mp->m_flags |=XFS_MOUNT_FS_SHUTDOWN ; +if (mp->m_sb_bp) + mp->m_sb_bp->b_flags |=XBF_DONE ; +spin_unlock (&mp->m_sb_lock); + +if (flags &SHUTDOWN_FORCE_UMOUNT ) +xfs_alert (mp,"User initiated shutdown received." ); -if (flags &SHUTDOWN_CORRUPT_INCORE ) { -xfs_alert_tag (mp,XFS_PTAG_SHUTDOWN_CORRUPT , -"Corruption of in-memory data (0x%x) detected at %pS (%s:%d). Shutting down filesystem" , - flags, __return_address, fname, lnnum); -if (XFS_ERRLEVEL_HIGH <= xfs_error_level) -xfs_stack_trace (); - }else if (logerror) { -xfs_alert_tag (mp,XFS_PTAG_SHUTDOWN_LOGERROR , -"Log I/O error (0x%x) detected at %pS (%s:%d). Shutting down filesystem" , - flags, __return_address, fname, lnnum); +if (xlog_force_shutdown (mp->m_log, flags)) { + tag =XFS_PTAG_SHUTDOWN_LOGERROR ; + why ="Log I/O Error" ; + }else if (flags &SHUTDOWN_CORRUPT_INCORE ) { + tag =XFS_PTAG_SHUTDOWN_CORRUPT ; + why ="Corruption of in-memory data" ; }else { -xfs_alert_tag (mp,XFS_PTAG_SHUTDOWN_IOERROR , -"I/O error (0x%x) detected at %pS (%s:%d). Shutting down filesystem" , - flags, __return_address, fname, lnnum); + tag =XFS_PTAG_SHUTDOWN_IOERROR ; + why ="Metadata I/O Error" ; } +xfs_alert_tag (mp, tag, +"%s (0x%x) detected at %pS (%s:%d). Shutting down filesystem." , + why, flags, __return_address, fname, lnnum);xfs_alert (mp,"Please unmount the filesystem and rectify the problem(s)" ); +if (xfs_error_level >=XFS_ERRLEVEL_HIGH ) +xfs_stack_trace (); }/*
fs/xfs/xfs_log.c +45 -55
128 lines not on this page · raw message
fs/xfs/xfs_log.h +1 -1
@@ -125,7 +125,6 @@ int xfs_log_reserve(struct xfs_mount *mp, bool permanent);int xfs_log_regrant (struct xfs_mount *mp,struct xlog_ticket *tic);void xfs_log_unmount (struct xfs_mount *mp); -int xfs_log_force_umount (struct xfs_mount *mp,int logerror);bool xfs_log_writable (struct xfs_mount *mp);struct xlog_ticket *xfs_log_ticket_get (struct xlog_ticket *ticket);
@@ -140,6 +139,7 @@ void xfs_log_clean(struct xfs_mount *mp);bool xfs_log_check_lsn (struct xfs_mount *,xfs_lsn_t );xfs_lsn_t xlog_grant_push_threshold (struct xlog *log,int need_bytes); +bool xlog_force_shutdown (struct xlog *log,int shutdown_flags);void xlog_use_incompat_feat (struct xlog *log);void xlog_drop_incompat_feat (struct xlog *log);
Signature · 1 line
2.31.1
PATCH v3 6/9 xfs: rework xlog_state_do_callback()
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Clean it up a bit by factoring and rearranging some of the code.
--- fs/xfs/xfs_log.c | 96 ++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 43 deletions(-)
fs/xfs/xfs_log.c +53 -43
@@ -2838,56 +2838,66 @@ xlog_state_iodone_process_iclog( } } +/* +* Loop over all the iclogs, running attached callbacks on them. Return true if +* we ran any callbacks, indicating that we dropped the icloglock. +*/ +static bool +xlog_state_do_iclog_callbacks ( +struct xlog *log) + __releases(&log->l_icloglock) + __acquires(&log->l_icloglock) +{ +struct xlog_in_core *first_iclog = log->l_iclog; +struct xlog_in_core *iclog = first_iclog; +bool ran_callback = false; + +do { +LIST_HEAD (cb_list); + +if (!xlog_is_shutdown (log)) { +if (xlog_state_iodone_process_iclog (log, iclog)) +break ; +if (iclog->ic_state !=XLOG_STATE_CALLBACK ) { + iclog = iclog->ic_next; +continue ; + } + } +list_splice_init (&iclog->ic_callbacks, &cb_list); +spin_unlock (&log->l_icloglock); + +trace_xlog_iclog_callbacks_start (iclog, _RET_IP_); +xlog_cil_process_committed (&cb_list); +trace_xlog_iclog_callbacks_done (iclog, _RET_IP_); + ran_callback = true; + +spin_lock (&log->l_icloglock); +if (xlog_is_shutdown (log)) +wake_up_all (&iclog->ic_force_wait); +else +xlog_state_clean_iclog (log, iclog); + iclog = iclog->ic_next; + }while (iclog != first_iclog); + +return ran_callback; +} + + +/* +* Loop running iclog completion callbacks until there are no more iclogs in a +* state that can run callbacks. +*/ STATIC voidxlog_state_do_callback (struct xlog *log) { -struct xlog_in_core *iclog; -struct xlog_in_core *first_iclog; -bool cycled_icloglock;int flushcnt =0 ;int repeats =0 ;spin_lock (&log->l_icloglock); - do { - /* - * Scan all iclogs starting with the one pointed to by the - * log. Reset this starting point each time the log is - * unlocked (during callbacks). - * - * Keep looping through iclogs until one full pass is made - * without running any callbacks. - */ - cycled_icloglock = false; - first_iclog = log->l_iclog; - iclog = first_iclog; - - do { - LIST_HEAD(cb_list); - - if (!xlog_is_shutdown(log)) { - if (xlog_state_iodone_process_iclog(log, iclog)) - break; - if (iclog->ic_state != XLOG_STATE_CALLBACK) { - iclog = iclog->ic_next; - continue; - } - } - list_splice_init(&iclog->ic_callbacks, &cb_list); - spin_unlock(&log->l_icloglock); - - trace_xlog_iclog_callbacks_start(iclog, _RET_IP_); - xlog_cil_process_committed(&cb_list); - trace_xlog_iclog_callbacks_done(iclog, _RET_IP_); - cycled_icloglock = true; - - spin_lock(&log->l_icloglock); - if (xlog_is_shutdown(log)) - wake_up_all(&iclog->ic_force_wait); - else - xlog_state_clean_iclog(log, iclog); - iclog = iclog->ic_next; - } while (iclog != first_iclog); +while (xlog_state_do_iclog_callbacks (log)) { +if (xlog_is_shutdown (log)) +break ;if (++repeats >5000 ) { flushcnt += repeats;
@@ -2896,7 +2906,7 @@ xlog_state_do_callback("%s: possible infinite loop (%d iterations)" , __func__, flushcnt); } - }while (!xlog_is_shutdown (log) && cycled_icloglock); + }if (log->l_iclog->ic_state ==XLOG_STATE_ACTIVE ||xlog_is_shutdown (log))
Signature · 1 line
2.31.1
PATCH v3 7/9 xfs: separate out log shutdown callback processing
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
The iclog callback processing done during a forced log shutdown has different logic to normal runtime IO completion callback processing. Separate out the shutdown callbacks into their own function and call that from the shutdown code instead.
We don't need this shutdown specific logic in the normal runtime completion code - we'll always run the shutdown version on shutdown, and it will do what shutdown needs regardless of whether there are racing IO completion callbacks scheduled or in progress. Hence we can also simplify the normal IO completion callpath and only abort if shutdown occurred while we actively were processing callbacks.
Further, separating out the IO completion logic from the shutdown logic avoids callback race conditions from being triggered by log IO completion after a shutdown. IO completion will now only run callbacks on iclogs that are in the correct state for a callback to be run, avoiding the possibility of running callbacks on a referenced iclog that hasn't yet been submitted for IO.
--- fs/xfs/xfs_log.c | 53 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 15 deletions(-)
fs/xfs/xfs_log.c +38 -15
@@ -486,6 +486,32 @@ xfs_log_reserve(return error; } +/* +* Run all the pending iclog callbacks and wake log force waiters and iclog +* space waiters so they can process the newly set shutdown state. We really +* don't care what order we process callbacks here because the log is shut down +* and so state cannot change on disk anymore. +*/ +static void +xlog_state_shutdown_callbacks ( +struct xlog *log) +{ +struct xlog_in_core *iclog; +LIST_HEAD (cb_list); + +spin_lock (&log->l_icloglock); + iclog = log->l_iclog; +do { +list_splice_init (&iclog->ic_callbacks, &cb_list); +wake_up_all (&iclog->ic_force_wait); + }while ((iclog = iclog->ic_next) != log->l_iclog); + +wake_up_all (&log->l_flush_wait); +spin_unlock (&log->l_icloglock); + +xlog_cil_process_committed (&cb_list); +} +/* * Flush iclog to disk if this is the last reference to the given iclog and the * it is in the WANT_SYNC state.
@@ -2840,7 +2866,10 @@ xlog_state_iodone_process_iclog(/* * Loop over all the iclogs, running attached callbacks on them. Return true if -* we ran any callbacks, indicating that we dropped the icloglock. +* we ran any callbacks, indicating that we dropped the icloglock. We don't need +* to handle transient shutdown state here at all because +* xlog_state_shutdown_callbacks() will be run to do the necessary shutdown +* cleanup of the callbacks. */ static bool xlog_state_do_iclog_callbacks(
@@ -2855,13 +2884,11 @@ xlog_state_do_iclog_callbacks( do { LIST_HEAD(cb_list); - if (!xlog_is_shutdown(log)) { - if (xlog_state_iodone_process_iclog(log, iclog)) - break; - if (iclog->ic_state != XLOG_STATE_CALLBACK) { - iclog = iclog->ic_next; - continue; - } + if (xlog_state_iodone_process_iclog(log, iclog)) + break; + if (iclog->ic_state != XLOG_STATE_CALLBACK) { + iclog = iclog->ic_next; + continue; } list_splice_init(&iclog->ic_callbacks, &cb_list); spin_unlock(&log->l_icloglock);
@@ -2872,10 +2899,7 @@ xlog_state_do_iclog_callbacks( ran_callback = true;spin_lock (&log->l_icloglock); -if (xlog_is_shutdown (log)) -wake_up_all (&iclog->ic_force_wait); -else -xlog_state_clean_iclog (log, iclog); +xlog_state_clean_iclog (log, iclog); iclog = iclog->ic_next; }while (iclog != first_iclog);
@@ -2908,8 +2932,7 @@ xlog_state_do_callback( } } -if (log->l_iclog->ic_state ==XLOG_STATE_ACTIVE || -xlog_is_shutdown (log)) +if (log->l_iclog->ic_state ==XLOG_STATE_ACTIVE )wake_up_all (&log->l_flush_wait);spin_unlock (&log->l_icloglock);
@@ -3885,7 +3908,7 @@ xlog_force_shutdown(spin_lock (&log->l_cilp->xc_push_lock);wake_up_all (&log->l_cilp->xc_commit_wait);spin_unlock (&log->l_cilp->xc_push_lock); -xlog_state_do_callback (log); +xlog_state_shutdown_callbacks (log);return log_error; }
Signature · 1 line
2.31.1
PATCH v3 8/9 xfs: don't run shutdown callbacks on active iclogs
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
When the log is shutdown, it currently walks all the iclogs and runs callbacks that are attached to the iclogs, regardless of whether the iclog is queued for IO completion or not. This creates a problem for contexts attaching callbacks to iclogs in that a racing shutdown can run the callbacks even before the attaching context has finished processing the iclog and releasing it for IO submission.
If the callback processing of the iclog frees the structure that is attached to the iclog, then this leads to an UAF scenario that can only be protected against by holding the icloglock from the point callbacks are attached through to the release of the iclog. While we currently do this, it is not practical or sustainable.
Hence we need to make shutdown processing the responsibility of the context that holds active references to the iclog. We know that the contexts attaching callbacks to the iclog must have active references to the iclog, and that means they must be in either ACTIVE or WANT_SYNC states. xlog_state_do_callback() will skip over iclogs in these states -except- when the log is shut down.
xlog_state_do_callback() checks the state of the iclogs while holding the icloglock, therefore the reference count/state change that occurs in xlog_state_release_iclog() after the callbacks are atomic w.r.t. shutdown processing.
We can't push the responsibility of callback cleanup onto the CIL context because we can have ACTIVE iclogs that have callbacks attached that have already been released. Hence we really need to internalise the cleanup of callbacks into xlog_state_release_iclog() processing.
Indeed, we already have that internalisation via:
xlog_state_release_iclog
drop last reference
->SYNCING
xlog_sync
xlog_write_iclog
if (log_is_shutdown)
xlog_state_done_syncing()
xlog_state_do_callback()
<process shutdown on iclog that is now in SYNCING state>
The problem is that xlog_state_release_iclog() aborts before doing anything if the log is already shut down. It assumes that the callbacks have already been cleaned up, and it doesn't need to do any cleanup.
Hence the fix is to remove the xlog_is_shutdown() check from xlog_state_release_iclog() so that reference counts are correctly released from the iclogs, and when the reference count is zero we always transition to SYNCING if the log is shut down. Hence we'll always enter the xlog_sync() path in a shutdown and eventually end up erroring out the iclog IO and running xlog_state_do_callback() to process the callbacks attached to the iclog.
This allows us to stop processing referenced ACTIVE/WANT_SYNC iclogs directly in the shutdown code, and in doing so gets rid of the UAF vector that currently exists. This then decouples the adding of callbacks to the iclogs from xlog_state_release_iclog() as we guarantee that xlog_state_release_iclog() will process the callbacks if the log has been shut down before xlog_state_release_iclog() has been called.
--- fs/xfs/xfs_log.c | 35 +++++++++++++++++++++++++++++++---- fs/xfs/xfs_log_cil.c | 15 +++++++-------- 2 files changed, 38 insertions(+), 12 deletions(-)
fs/xfs/xfs_log.c +31 -4
@@ -41,6 +41,8 @@ xlog_dealloc_log(/* local state machine functions */ STATIC void xlog_state_done_syncing(struct xlog_in_core *iclog); +STATIC void xlog_state_do_callback( +struct xlog *log); STATIC int xlog_state_get_iclog_space( struct xlog *log,
@@ -491,6 +493,11 @@ xfs_log_reserve(* space waiters so they can process the newly set shutdown state. We really * don't care what order we process callbacks here because the log is shut down * and so state cannot change on disk anymore. +* +* We avoid processing actively referenced iclogs so that we don't run callbacks +* while the iclog owner might still be preparing the iclog for IO submssion. +* These will be caught by xlog_state_iclog_release() and call this function +* again to process any callbacks that may have been added to that iclog. */ static void xlog_state_shutdown_callbacks(
@@ -502,7 +509,12 @@ xlog_state_shutdown_callbacks(spin_lock (&log->l_icloglock); iclog = log->l_iclog;do { +if (atomic_read (&iclog->ic_refcnt)) { +/* Reference holder will re-run iclog callbacks. */ +continue ; + }list_splice_init (&iclog->ic_callbacks, &cb_list); +wake_up_all (&iclog->ic_write_wait);wake_up_all (&iclog->ic_force_wait); }while ((iclog = iclog->ic_next) != log->l_iclog);
@@ -546,12 +558,11 @@ xlog_state_release_iclog( xfs_lsn_t old_tail_lsn) {xfs_lsn_t tail_lsn; +bool last_ref; +lockdep_assert_held (&log->l_icloglock);trace_xlog_iclog_release (iclog, _RET_IP_); -if (xlog_is_shutdown (log)) -return -EIO ; -/* * Grabbing the current log tail needs to be atomic w.r.t. the writing * of the tail LSN into the iclog so we guarantee that the log tail does
@@ -569,7 +580,23 @@ xlog_state_release_iclog( iclog->ic_header.h_tail_lsn =cpu_to_be64 (tail_lsn); } -if (!atomic_dec_and_test (&iclog->ic_refcnt)) + last_ref =atomic_dec_and_test (&iclog->ic_refcnt); + +if (xlog_is_shutdown (log)) { +/* +* If there are no more references to this iclog, process the +* pending iclog callbacks that were waiting on the release of +* this iclog. +*/ +if (last_ref) { +spin_unlock (&log->l_icloglock); +xlog_state_shutdown_callbacks (log); +spin_lock (&log->l_icloglock); + } +return -EIO ; + } + +if (!last_ref)return 0 ;if (iclog->ic_state !=XLOG_STATE_WANT_SYNC ) {
fs/xfs/xfs_log_cil.c +7 -8
@@ -891,11 +891,10 @@ xlog_cil_push_work(xfs_log_ticket_ungrant (log, tic);/* -* Once we attach the ctx to the iclog, a shutdown can process the -* iclog, run the callbacks and free the ctx. The only thing preventing -* this potential UAF situation here is that we are holding the -* icloglock. Hence we cannot access the ctx once we have attached the -* callbacks and dropped the icloglock. +* Once we attach the ctx to the iclog, it is effectively owned by the +* iclog and we can only use it while we still have an active reference +* to the iclog. i.e. once we call xlog_state_release_iclog() we can no +* longer safely reference the ctx. */ spin_lock (&log->l_icloglock);if (xlog_is_shutdown (log)) {
@@ -927,9 +926,6 @@ xlog_cil_push_work(* wakeup until this commit_iclog is written to disk. Hence we use the * iclog header lsn and compare it to the commit lsn to determine if we * need to wait on iclogs or not. -* -* NOTE: It is not safe to reference the ctx after this check as we drop -* the icloglock if we have to wait for completion of other iclogs. */ if (ctx->start_lsn != commit_lsn) {xfs_lsn_t plsn;
@@ -959,6 +955,9 @@ xlog_cil_push_work(*/ commit_iclog->ic_flags |=XLOG_ICL_NEED_FUA ;xlog_state_release_iclog (log, commit_iclog, preflush_tail_lsn); + +/* Not safe to reference ctx now! */ +spin_unlock (&log->l_icloglock);return ;
Signature · 1 line
2.31.1
PATCH v3 9/9 xfs: log head and tail aren't reliable during shutdown
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
I'm seeing assert failures from xlog_space_left() after a shutdown has begun that look like:
XFS (dm-0): log I/O error -5 XFS (dm-0): xfs_do_force_shutdown(0x2) called from line 1338 of file fs/xfs/xfs_log.c. Return address = xlog_ioend_work+0x64/0xc0 XFS (dm-0): Log I/O Error Detected. XFS (dm-0): Shutting down filesystem. Please unmount the filesystem and rectify the problem(s) XFS (dm-0): xlog_space_left: head behind tail XFS (dm-0): tail_cycle = 6, tail_bytes = 2706944 XFS (dm-0): GH cycle = 6, GH bytes = 1633867 XFS: Assertion failed: 0, file: fs/xfs/xfs_log.c, line: 1310 ------------[ cut here ]------------ Call Trace: xlog_space_left+0xc3/0x110 xlog_grant_push_threshold+0x3f/0xf0 xlog_grant_push_ail+0x12/0x40 xfs_log_reserve+0xd2/0x270 ? __might_sleep+0x4b/0x80 xfs_trans_reserve+0x18b/0x260
.....
There are two things here. Firstly, after a shutdown, the log head and tail can be out of whack as things abort and release (or don't release) resources, so checking them for sanity doesn't make much sense. Secondly, xfs_log_reserve() can race with shutdown and so it can still fail like this even though it has already checked for a log shutdown before calling xlog_grant_push_ail().
So, before ASSERT failing in xlog_space_left(), make sure we haven't already shut down....
--- fs/xfs/xfs_log.c | 51 +++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 24 deletions(-)
fs/xfs/xfs_log.c +27 -24
@@ -1307,16 +1307,18 @@ xlog_assign_tail_lsn(* wrap the tail, we should blow up. Rather than catch this case here, * we depend on other ASSERTions in other parts of the code. XXXmiken * -* This code also handles the case where the reservation head is behind -* the tail. The details of this case are described below, but the end -* result is that we return the size of the log as the amount of space left. +* If reservation head is behind the tail, we have a problem. Warn about it, +* but then treat it as if the log is empty. +* +* If the log is shut down, the head and tail may be invalid or out of whack, so +* shortcut invalidity asserts in this case so that we don't trigger them +* falsely. */ STATIC intxlog_space_left (struct xlog *log,atomic64_t *head) { -int free_bytes;int tail_bytes;int tail_cycle;int head_cycle;
@@ -1326,29 +1328,30 @@ xlog_space_left(xlog_crack_atomic_lsn (&log->l_tail_lsn, &tail_cycle, &tail_bytes); tail_bytes =BBTOB (tail_bytes);if (tail_cycle == head_cycle && head_bytes >= tail_bytes) - free_bytes = log->l_logsize - (head_bytes - tail_bytes); -else if (tail_cycle +1 < head_cycle) +return log->l_logsize - (head_bytes - tail_bytes); +if (tail_cycle +1 < head_cycle)return 0 ; -else if (tail_cycle < head_cycle) { + +/* Ignore potential inconsistency when shutdown. */ +if (xlog_is_shutdown (log)) +return log->l_logsize; + +if (tail_cycle < head_cycle) {ASSERT (tail_cycle == (head_cycle -1 )); - free_bytes = tail_bytes - head_bytes; - }else { -/* -* The reservation head is behind the tail. -* In this case we just want to return the size of the -* log as the amount of space left. -*/ -xfs_alert (log->l_mp,"xlog_space_left: head behind tail" ); -xfs_alert (log->l_mp, -" tail_cycle = %d, tail_bytes = %d" , - tail_cycle, tail_bytes); -xfs_alert (log->l_mp, -" GH cycle = %d, GH bytes = %d" , - head_cycle, head_bytes); -ASSERT (0 ); - free_bytes = log->l_logsize; +return tail_bytes - head_bytes; } -return free_bytes; + +/* +* The reservation head is behind the tail. In this case we just want to +* return the size of the log as the amount of space left. +*/ +xfs_alert (log->l_mp,"xlog_space_left: head behind tail" ); +xfs_alert (log->l_mp," tail_cycle = %d, tail_bytes = %d" , + tail_cycle, tail_bytes); +xfs_alert (log->l_mp," GH cycle = %d, GH bytes = %d" , + head_cycle, head_bytes); +ASSERT (0 ); +return log->l_logsize; }
Signature · 1 line
2.31.1