Skip to content

Commit 7a249cf

Browse files
author
Christoph Hellwig
committed
xfs: fix filesystsem freeze race in xfs_trans_alloc
As pointed out by Jan xfs_trans_alloc can race with a concurrent filesystem freeze when it sleeps during the memory allocation. Fix this by moving the wait_for_freeze call after the memory allocation. This means moving the freeze into the low-level _xfs_trans_alloc helper, which thus grows a new argument. Also fix up some comments in that area while at it. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Alex Elder <[email protected]> Reviewed-by: Dave Chinner <[email protected]>
1 parent 33b8f7c commit 7a249cf

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

fs/xfs/xfs_fsops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ xfs_fs_log_dummy(
626626
xfs_trans_t *tp;
627627
int error;
628628

629-
tp = _xfs_trans_alloc(mp, XFS_TRANS_DUMMY1, KM_SLEEP);
629+
tp = _xfs_trans_alloc(mp, XFS_TRANS_DUMMY1, KM_SLEEP, false);
630630
error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
631631
XFS_DEFAULT_LOG_COUNT);
632632
if (error) {

fs/xfs/xfs_iomap.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,7 @@ xfs_iomap_write_unwritten(
688688
* the same inode that we complete here and might deadlock
689689
* on the iolock.
690690
*/
691-
xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
692-
tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
691+
tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS, true);
693692
tp->t_flags |= XFS_TRANS_RESERVE;
694693
error = xfs_trans_reserve(tp, resblks,
695694
XFS_WRITE_LOG_RES(mp), 0,

fs/xfs/xfs_mount.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,15 +1566,9 @@ xfs_fs_writable(xfs_mount_t *mp)
15661566
}
15671567

15681568
/*
1569-
* xfs_log_sbcount
1570-
*
15711569
* Called either periodically to keep the on disk superblock values
15721570
* roughly up to date or from unmount to make sure the values are
15731571
* correct on a clean unmount.
1574-
*
1575-
* Note this code can be called during the process of freezing, so
1576-
* we may need to use the transaction allocator which does not not
1577-
* block when the transaction subsystem is in its frozen state.
15781572
*/
15791573
int
15801574
xfs_log_sbcount(
@@ -1596,7 +1590,13 @@ xfs_log_sbcount(
15961590
if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
15971591
return 0;
15981592

1599-
tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
1593+
/*
1594+
* We can be called during the process of freezing, so make sure
1595+
* we go ahead even if the frozen for new transactions. We will
1596+
* always use a sync transaction in the freeze path to make sure
1597+
* the transaction has completed by the time we return.
1598+
*/
1599+
tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP, false);
16001600
error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
16011601
XFS_DEFAULT_LOG_COUNT);
16021602
if (error) {

fs/xfs/xfs_trans.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -566,31 +566,24 @@ xfs_trans_init(
566566

567567
/*
568568
* This routine is called to allocate a transaction structure.
569+
*
569570
* The type parameter indicates the type of the transaction. These
570571
* are enumerated in xfs_trans.h.
571-
*
572-
* Dynamically allocate the transaction structure from the transaction
573-
* zone, initialize it, and return it to the caller.
574572
*/
575-
xfs_trans_t *
576-
xfs_trans_alloc(
577-
xfs_mount_t *mp,
578-
uint type)
579-
{
580-
xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
581-
return _xfs_trans_alloc(mp, type, KM_SLEEP);
582-
}
583-
584-
xfs_trans_t *
573+
struct xfs_trans *
585574
_xfs_trans_alloc(
586-
xfs_mount_t *mp,
587-
uint type,
588-
uint memflags)
575+
struct xfs_mount *mp,
576+
uint type,
577+
uint memflags,
578+
bool wait_for_freeze)
589579
{
590-
xfs_trans_t *tp;
580+
struct xfs_trans *tp;
591581

592582
atomic_inc(&mp->m_active_trans);
593583

584+
if (wait_for_freeze)
585+
xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
586+
594587
tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
595588
tp->t_magic = XFS_TRANS_MAGIC;
596589
tp->t_type = type;

fs/xfs/xfs_trans.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,14 @@ typedef struct xfs_trans {
447447
/*
448448
* XFS transaction mechanism exported interfaces.
449449
*/
450-
xfs_trans_t *xfs_trans_alloc(struct xfs_mount *, uint);
451-
xfs_trans_t *_xfs_trans_alloc(struct xfs_mount *, uint, uint);
450+
xfs_trans_t *_xfs_trans_alloc(struct xfs_mount *, uint, uint, bool);
451+
452+
static inline struct xfs_trans *
453+
xfs_trans_alloc(struct xfs_mount *mp, uint type)
454+
{
455+
return _xfs_trans_alloc(mp, type, KM_SLEEP, true);
456+
}
457+
452458
xfs_trans_t *xfs_trans_dup(xfs_trans_t *);
453459
int xfs_trans_reserve(xfs_trans_t *, uint, uint, uint,
454460
uint, uint);

0 commit comments

Comments
 (0)