xfs: clear MS_ACTIVE after finishing log recovery to avoid inode leak
PATCH 1/3 xfs: clear MS_ACTIVE after finishing log recovery to avoid inode leak
Way back when we established inode block-map redo log items, it was discovered that we needed to prevent the VFS from evicting inodes during log recovery because any given inode might be have bmap redo items to replay even if the inode has no link count and is ultimately deleted, and any eviction of an unlinked inode causes the inode to be truncated and freed too early.
To make this possible, we set MS_ACTIVE so that inodes would not be torn down immediately upon release. Unfortunately, this also results in the quota inodes not being released at all if a later part of the mount process should fail, because we never reclaim the inodes. So, clear MS_ACTIVE immediately after we finish the log recovery so that the quota inodes will be torn down properly if we abort the mount.
--- fs/xfs/xfs_mount.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/xfs/xfs_mount.c +4 -2
@@ -949,7 +949,9 @@ xfs_mountfs(* iput to behave like they do for an active filesystem. * xfs_fs_drop_inode needs to be able to prevent the deletion * of inodes before we're done replaying log items on those -* inodes. +* inodes. Turn it off immediately after xfs_log_mount_finish +* so that we don't leak the quota inodes if subsequent mount +* activities fail. */ mp->m_super->s_flags |=MS_ACTIVE ;
@@ -959,6 +961,7 @@ xfs_mountfs(* read in. */ error =xfs_log_mount_finish (mp); + mp->m_super->s_flags &= ~MS_ACTIVE ;if (error) {xfs_warn (mp,"log mount finish failed" ); goto out_rtunmount;
@@ -1028,7 +1031,6 @@ xfs_mountfs( out_quota:xfs_qm_unmount_quotas (mp); out_rtunmount: - mp->m_super->s_flags &= ~MS_ACTIVE ;xfs_rtunmount_inodes (mp); out_rele_rip:IRELE (rip);
PATCH 2/3 xfs: don't leak linked inodes during log recovery
When we introduced the bmap redo log items, we set MS_ACTIVE on the mountpoint and XFS_IRECOVERY on the inode to prevent unlinked inodes from being truncated prematurely during log recovery. However, we neglected to drop linked inodes that are recovered, and if we don't use the inode between recovery and unmount, the inode will never be marked reclaimable and thus we fail to free it at umount time. If we're in log recovery but IRECOVERY is /not/ set, the inode is linked and can be reclaimed.
--- fs/xfs/xfs_super.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/xfs/xfs_super.c +7 -0
@@ -1040,6 +1040,13 @@ xfs_fs_drop_inode(if (ip->i_flags &XFS_IRECOVERY ) {ASSERT (ip->i_mount->m_log->l_flags &XLOG_RECOVERY_NEEDED );return 0 ; + }else if (ip->i_mount->m_log->l_flags &XLOG_RECOVERY_NEEDED ) { +/* +* This inode was loaded during recovery but is not +* being unlinked, so we can free it without fear of +* premature truncation. +*/ +return 1 ; }return generic_drop_inode (inode) || (ip->i_flags &XFS_IDONTCACHE );
Re: [PATCH 2/3] xfs: don't leak linked inodes during log recovery
Quoted 9 lines · Darrick J. Wong
When we introduced the bmap redo log items, we set
MS_ACTIVEon the mountpoint andXFS_IRECOVERYon the inode to prevent unlinked inodes from being truncated prematurely during log recovery. However, we neglected to drop linked inodes that are recovered, and if we don't use the inode between recovery and unmount, the inode will never be marked reclaimable and thus we fail to free it at umount time. If we're in log recovery but IRECOVERY is /not/ set, the inode is linked and can be reclaimed.
I follow the change in behavior in the previous commit and how this restores the original behavior for linked inodes, so this patch makes sense from that perspective. I'm not following where/how the leak occurs from the description, however. Wouldn't the inode end up on the lru to be shrunk/evicted/reclaimed at a later point? What happens if the inode is subsequently used that prevents the leak? (Whatever I'm missing, it would be nice to elaborate on in the commit log.)
Also, if there is a memory leak vector for !drop linked inodes here, does that not apply to XFS_IRECOVERY inodes if log recovery itself happens to fail between bui recovery and iunlink processing?
Brian
Quoted 28 lines
Fixes 17c12bcd30 ("xfs: when replaying bmap operations, don't let unlinked inodes get reaped")Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_super.c | 7 +++++++ 1 file changed, 7 insertions(+)fs/xfs/xfs_super.c +7
-1@@ -1040,6 +1040,13 @@ xfs_fs_drop_inode(if (ip->i_flags &XFS_IRECOVERY ) {ASSERT (ip->i_mount->m_log->l_flags &XLOG_RECOVERY_NEEDED );return 0 ; + }else if (ip->i_mount->m_log->l_flags &XLOG_RECOVERY_NEEDED ) { +/* +* This inode was loaded during recovery but is not +* being unlinked, so we can free it without fear of +* premature truncation. +*/ +return 1 ; }return generic_drop_inode (inode) || (ip->i_flags &XFS_IDONTCACHE ); --To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 2/3] xfs: don't leak linked inodes during log recovery
Quoted 11 lines · Brian Foster
Quoted 9 lines · Darrick J. Wong
When we introduced the bmap redo log items, we set
MS_ACTIVEon the mountpoint andXFS_IRECOVERYon the inode to prevent unlinked inodes from being truncated prematurely during log recovery. However, we neglected to drop linked inodes that are recovered, and if we don't use the inode between recovery and unmount, the inode will never be marked reclaimable and thus we fail to free it at umount time. If we're in log recovery but IRECOVERY is /not/ set, the inode is linked and can be reclaimed.
Quoted 4 lines
I follow the change in behavior in the previous commit and how this restores the original behavior for linked inodes, so this patch makes sense from that perspective. I'm not following where/how the leak occurs from the description, however.
Linked inodes are inode_add_lru()'d, but nothing ever calls evict_inodes() to clean up that lru (sb->s_inodes).
Quoted 6 lines
Wouldn't the inode end up on the lru to be shrunk/evicted/reclaimed at a later point?
What happens if the inode is subsequently used that prevents the leak? (Whatever I'm missing, it would be nice to elaborate on in the commit log.)
If we make it all the way to a successful mount, then an unmount can call generic_shutdown_super -> evict_inodes to clean up all the inodes on the lru list.
Quoted 3 lines
Also, if there is a memory leak vector for !drop linked inodes here, does that not apply to
XFS_IRECOVERYinodes if log recovery itself happens to fail between bui recovery and iunlink processing?
Ugh, I forgot about that possibility. I think the solution is to evict_inodes right after we clear MS_ACTIVE but before we see if xfs_log_mount_finish actually failed.
--D
Quoted 35 lines
Brian
Quoted 25 lines
Fixes 17c12bcd30 ("xfs: when replaying bmap operations, don't let unlinked inodes get reaped")Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_super.c | 7 +++++++ 1 file changed, 7 insertions(+)fs/xfs/xfs_super.c +7
-1@@ -1040,6 +1040,13 @@ xfs_fs_drop_inode(if (ip->i_flags &XFS_IRECOVERY ) {ASSERT (ip->i_mount->m_log->l_flags &XLOG_RECOVERY_NEEDED );return 0 ; + }else if (ip->i_mount->m_log->l_flags &XLOG_RECOVERY_NEEDED ) { +/* +* This inode was loaded during recovery but is not +* being unlinked, so we can free it without fear of +* premature truncation. +*/ +return 1 ; }return generic_drop_inode (inode) || (ip->i_flags &XFS_IDONTCACHE ); --Quoted 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 2/3] xfs: don't leak linked inodes during log recovery
Quoted 23 lines · Darrick J. Wong
Quoted 11 lines · Brian Foster
Quoted 9 lines · Darrick J. Wong
When we introduced the bmap redo log items, we set
MS_ACTIVEon the mountpoint andXFS_IRECOVERYon the inode to prevent unlinked inodes from being truncated prematurely during log recovery. However, we neglected to drop linked inodes that are recovered, and if we don't use the inode between recovery and unmount, the inode will never be marked reclaimable and thus we fail to free it at umount time. If we're in log recovery but IRECOVERY is /not/ set, the inode is linked and can be reclaimed.Quoted 4 lines
I follow the change in behavior in the previous commit and how this restores the original behavior for linked inodes, so this patch makes sense from that perspective. I'm not following where/how the leak occurs from the description, however.
Linked inodes are inode_add_lru()'d, but nothing ever calls
evict_inodes()to clean up that lru (sb->s_inodes).Quoted 2 lines
Wouldn't the inode end up on the lru to be shrunk/evicted/reclaimed at a later point?
Quoted 8 lines
Quoted 3 lines
What happens if the inode is subsequently used that prevents the leak? (Whatever I'm missing, it would be nice to elaborate on in the commit log.)
If we make it all the way to a successful mount, then an unmount can call
generic_shutdown_super->evict_inodesto clean up all the inodes on the lru list.
Ok, so you are saying that the leak occurs only if the mount ends up failing because the vfs doesn't expect to have to evict lru inodes until it sets the sb active, right? If so, that makes sense.. thanks.
FWIW, I think it's just more that the patch description is a bit confusing here than anything. It doesn't mention mount failure and I'm still a little confused about what you mean by: "if we don't use the inode between recovery and unmount, the inode will never be marked reclaimable and thus we fail to free it at umount time." Does that mean that assuming imminent mount failure, the inode is leaked unless something else happens to lookup/release the inode after we clear MS_ACTIVE but before the mount failure sequence completes?
Quoted 8 lines
Quoted 3 lines
Also, if there is a memory leak vector for !drop linked inodes here, does that not apply to
XFS_IRECOVERYinodes if log recovery itself happens to fail between bui recovery and iunlink processing?Ugh, I forgot about that possibility. I think the solution is to
evict_inodesright after we clearMS_ACTIVEbut before we see ifxfs_log_mount_finishactually failed.
That sounds reasonable. If we do that, I suppose that means we can drop the additional logic in ->drop_inode() because that would cover both linked and unlinked inodes that make it onto the lru during recovery.
Brian
Quoted 41 lines
--D
Quoted 26 lines
Brian
Quoted 23 lines
Fixes 17c12bcd30 ("xfs: when replaying bmap operations, don't let unlinked inodes get reaped")Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_super.c | 7 +++++++ 1 file changed, 7 insertions(+)fs/xfs/xfs_super.c +7
-0@@ -1040,6 +1040,13 @@ xfs_fs_drop_inode(if (ip->i_flags &XFS_IRECOVERY ) {ASSERT (ip->i_mount->m_log->l_flags &XLOG_RECOVERY_NEEDED );return 0 ; + }else if (ip->i_mount->m_log->l_flags &XLOG_RECOVERY_NEEDED ) { +/* +* This inode was loaded during recovery but is not +* being unlinked, so we can free it without fear of +* premature truncation. +*/ +return 1 ; }Quoted 9 lines
Quoted 5 lines
fs/xfs/xfs_super.c +0
-1return generic_drop_inode (inode) || (ip->i_flags &XFS_IDONTCACHE ); --To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 3/3 xfs: don't leak quotacheck dquots when cow recovery fails
In reply to Darrick J. WongIf we fail a mount on account of cow recovery errors, it's possible that a previous quotacheck left some dquots in memory. The bailout clause of xfs_mountfs forgets to purge these, and so we leak them. Fix that.
--- fs/xfs/xfs_mount.c | 2 ++ 1 file changed, 2 insertions(+)
fs/xfs/xfs_mount.c +2 -0
@@ -1029,6 +1029,8 @@ xfs_mountfs( out_agresv:xfs_fs_unreserve_ag_blocks (mp); out_quota: +/* Clean out dquots that might be in memory after quotacheck. */ +xfs_qm_unmount (mp);xfs_qm_unmount_quotas (mp); out_rtunmount:xfs_rtunmount_inodes (mp);
Re: [PATCH 3/3] xfs: don't leak quotacheck dquots when cow recovery fails
Quoted 14 lines · Darrick J. Wong
If we fail a mount on account of cow recovery errors, it's possible that a previous quotacheck left some dquots in memory. The bailout clause of
xfs_mountfsforgets to purge these, and so we leak them. Fix that.Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_mount.c | 2 ++ 1 file changed, 2 insertions(+)fs/xfs/xfs_mount.c +0
-0
fs/xfs/xfs_mount.c xfs_mountfs( · line 1034
Quoted 6 lines
fs/xfs/xfs_mount.c +2
-0@@ -1029,6 +1029,8 @@ xfs_mountfs( out_agresv:xfs_fs_unreserve_ag_blocks (mp); out_quota: +/* Clean out dquots that might be in memory after quotacheck. */ +xfs_qm_unmount (mp);xfs_qm_unmount_quotas (mp);
Should these calls be reversed? It looks like qm_unmount() can free m_quotainfo before qm_unmount_quotas() has a chance to release quota inodes and whatnot.
Brian
Quoted 6 lines
fs/xfs/xfs_mount.c +0
-1out_rtunmount:xfs_rtunmount_inodes (mp); --To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 3/3] xfs: don't leak quotacheck dquots when cow recovery fails
Quoted 10 lines · Brian Foster
Quoted 9 lines · Darrick J. Wong
If we fail a mount on account of cow recovery errors, it's possible that a previous quotacheck left some dquots in memory. The bailout clause of
xfs_mountfsforgets to purge these, and so we leak them. Fix that.Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_mount.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 15 lines
fs/xfs/xfs_mount.c xfs_mountfs( · line 1034
Quoted 11 lines
fs/xfs/xfs_mount.c +2
-0@@ -1029,6 +1029,8 @@ xfs_mountfs( out_agresv:xfs_fs_unreserve_ag_blocks (mp); out_quota: +/* Clean out dquots that might be in memory after quotacheck. */ +xfs_qm_unmount (mp);xfs_qm_unmount_quotas (mp);Should these calls be reversed? It looks like
qm_unmount()can freem_quotainfobeforeqm_unmount_quotas()has a chance to release quota inodes and whatnot.
xfs_qm_unmount -> xfs_qm_destroy_quotainfo will free the quota inodes, but you're right that we need (at least in theory) to call xfs_qm_unmount_quotas first so that we can xfs_qm_dqdetach the root/rbm/rsum inodes. I don't think we've actually attached dquots to those inodes at that point in the mount process (and the slab leak complaints have stopped) so at a practical level it might not matter.
Will change & resubmit,
--D
Quoted 13 lines
Brian
Quoted 6 lines
fs/xfs/xfs_mount.c +0
-1out_rtunmount:xfs_rtunmount_inodes (mp); --To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 14 lines · Darrick J. Wong
Way back when we established inode block-map redo log items, it was discovered that we needed to prevent the VFS from evicting inodes during log recovery because any given inode might be have bmap redo items to replay even if the inode has no link count and is ultimately deleted, and any eviction of an unlinked inode causes the inode to be truncated and freed too early.
To make this possible, we set
MS_ACTIVEso that inodes would not be torn down immediately upon release. Unfortunately, this also results in the quota inodes not being released at all if a later part of the mount process should fail, because we never reclaim the inodes. So, clearMS_ACTIVEimmediately after we finish the log recovery so that the quota inodes will be torn down properly if we abort the mount.
Quoted 2 lines
Fixes 17c12bcd30 ("xfs: when replaying bmap operations, don't let unlinked inodes get reaped")Signed-off-by Darrick J. Wong <[email protected]>
I think the whole series warrants:
Cc: <[email protected]> # 4.9
Quoted 36 lines · Darrick J. Wong
Way back when we established inode block-map redo log items, it was discovered that we needed to prevent the VFS from evicting inodes during log recovery because any given inode might be have bmap redo items to replay even if the inode has no link count and is ultimately deleted, and any eviction of an unlinked inode causes the inode to be truncated and freed too early.
To make this possible, we set
MS_ACTIVEso that inodes would not be torn down immediately upon release. Unfortunately, this also results in the quota inodes not being released at all if a later part of the mount process should fail, because we never reclaim the inodes. So, clearMS_ACTIVEimmediately after we finish the log recovery so that the quota inodes will be torn down properly if we abort the mount.Fixes 17c12bcd30 ("xfs: when replaying bmap operations, don't let unlinked inodes get reaped")Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_mount.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)fs/xfs/xfs_mount.c +3
-1@@ -949,7 +949,9 @@ xfs_mountfs(* iput to behave like they do for an active filesystem. * xfs_fs_drop_inode needs to be able to prevent the deletion * of inodes before we're done replaying log items on those -* inodes. +* inodes. Turn it off immediately after xfs_log_mount_finish +* so that we don't leak the quota inodes if subsequent mount +* activities fail. */ mp->m_super->s_flags |=MS_ACTIVE ;
fs/xfs/xfs_mount.c xfs_mountfs( · line 964
Quoted 4 lines
fs/xfs/xfs_mount.c +1
-0@@ -959,6 +961,7 @@ xfs_mountfs(* read in. */ error =xfs_log_mount_finish (mp); + mp->m_super->s_flags &= ~MS_ACTIVE ;
Just an aesthetic nit, but could we combine these two above hunks and the associated comments so the intent is very obvious? E.g., so it looks something like this:
/* * Set MS_ACTIVE around log recovery ... */ mp->m_super->s_flags |= MS_ACTIVE; error = xfs_log_mount_finish(mp); mp->m_super->s_flags &= ~MS_ACTIVE; ...
Otherwise this looks fine:
Quoted 15 lines
fs/xfs/xfs_mount.c +0
-2if (error) {xfs_warn (mp,"log mount finish failed" ); goto out_rtunmount;@@ -1028,7 +1031,6 @@ xfs_mountfs( out_quota:xfs_qm_unmount_quotas (mp); out_rtunmount: - mp->m_super->s_flags &= ~MS_ACTIVE ;xfs_rtunmount_inodes (mp); out_rele_rip:IRELE (rip); --To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 46 lines · Brian Foster
Quoted 36 lines · Darrick J. Wong
Way back when we established inode block-map redo log items, it was discovered that we needed to prevent the VFS from evicting inodes during log recovery because any given inode might be have bmap redo items to replay even if the inode has no link count and is ultimately deleted, and any eviction of an unlinked inode causes the inode to be truncated and freed too early.
To make this possible, we set
MS_ACTIVEso that inodes would not be torn down immediately upon release. Unfortunately, this also results in the quota inodes not being released at all if a later part of the mount process should fail, because we never reclaim the inodes. So, clearMS_ACTIVEimmediately after we finish the log recovery so that the quota inodes will be torn down properly if we abort the mount.Fixes 17c12bcd30 ("xfs: when replaying bmap operations, don't let unlinked inodes get reaped")Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_mount.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)fs/xfs/xfs_mount.c +3
-1@@ -949,7 +949,9 @@ xfs_mountfs(* iput to behave like they do for an active filesystem. * xfs_fs_drop_inode needs to be able to prevent the deletion * of inodes before we're done replaying log items on those -* inodes. +* inodes. Turn it off immediately after xfs_log_mount_finish +* so that we don't leak the quota inodes if subsequent mount +* activities fail. */ mp->m_super->s_flags |=MS_ACTIVE ;fs/xfs/xfs_mount.c xfs_mountfs( · line 964
Quoted 4 lines
fs/xfs/xfs_mount.c +1
-0@@ -959,6 +961,7 @@ xfs_mountfs(* read in. */ error =xfs_log_mount_finish (mp); + mp->m_super->s_flags &= ~MS_ACTIVE ;Just an aesthetic nit, but could we combine these two above hunks and the associated comments so the intent is very obvious? E.g., so it looks something like this:
Quoted 7 lines
/* * Set MS_ACTIVE around log recovery ... */ mp->m_super->s_flags |= MS_ACTIVE; error = xfs_log_mount_finish(mp); mp->m_super->s_flags &= ~MS_ACTIVE; ...
Yes, that's fine.
--D
Quoted 24 lines
Otherwise this looks fine:
Reviewed-by Brian Foster <[email protected]>Quoted 12 lines
fs/xfs/xfs_mount.c +0
-2if (error) {xfs_warn (mp,"log mount finish failed" ); goto out_rtunmount;@@ -1028,7 +1031,6 @@ xfs_mountfs( out_quota:xfs_qm_unmount_quotas (mp); out_rtunmount: - mp->m_super->s_flags &= ~MS_ACTIVE ;xfs_rtunmount_inodes (mp); out_rele_rip:IRELE (rip); --Quoted 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html