xfs: prevent stack overflows from page cache allocation
authorDave Chinner <dchinner@redhat.com>
Tue, 29 Oct 2013 11:11:57 +0000 (22:11 +1100)
committerJiri Slaby <jslaby@suse.cz>
Fri, 4 Jul 2014 08:06:56 +0000 (10:06 +0200)
commit ad22c7a043c2cc6792820e6c5da699935933e87d upstream.

Page cache allocation doesn't always go through ->begin_write and
hence we don't always get the opportunity to set the allocation
context to GFP_NOFS. Failing to do this means we open up the direct
relcaim stack to recurse into the filesystem and consume a
significant amount of stack.

On RHEL6.4 kernels we are seeing ra_submit() and
generic_file_splice_read() from an nfsd context recursing into the
filesystem via the inode cache shrinker and evicting inodes. This is
causing truncation to be run (e.g EOF block freeing) and causing
bmap btree block merges and free space btree block splits to occur.
These btree manipulations are occurring with the call chain already
30 functions deep and hence there is not enough stack space to
complete such operations.

To avoid these specific overruns, we need to prevent the page cache
allocation from recursing via direct reclaim. We can do that because
the allocation functions take the allocation context from that which
is stored in the mapping for the inode. We don't set that right now,
so the default is GFP_HIGHUSER_MOVABLE, which is effectively a
GFP_KERNEL context. We need it to be the equivalent of GFP_NOFS, so
when we initialise an inode, set the mapping gfp mask appropriately.

This makes the use of AOP_FLAG_NOFS redundant from other parts of
the XFS IO path, so get rid of it.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Ben Myers <bpm@sgi.com>
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
fs/xfs/xfs_aops.c
fs/xfs/xfs_iops.c

index e51e581454e93113c7ead8d81136ab82bd29da94..be9a1fa2721b25c59f75c034e026a0a68b086d48 100644 (file)
@@ -1569,8 +1569,7 @@ xfs_vm_write_begin(
 
        ASSERT(len <= PAGE_CACHE_SIZE);
 
-       page = grab_cache_page_write_begin(mapping, index,
-                                          flags | AOP_FLAG_NOFS);
+       page = grab_cache_page_write_begin(mapping, index, flags);
        if (!page)
                return -ENOMEM;
 
index 2b8952d9199bbd145473a48b326120b8d43ed9b6..584996c1bea75a339dde58f86a08f3735ba5e67b 100644 (file)
@@ -1169,6 +1169,7 @@ xfs_setup_inode(
        struct xfs_inode        *ip)
 {
        struct inode            *inode = &ip->i_vnode;
+       gfp_t                   gfp_mask;
 
        inode->i_ino = ip->i_ino;
        inode->i_state = I_NEW;
@@ -1228,6 +1229,14 @@ xfs_setup_inode(
                break;
        }
 
+       /*
+        * Ensure all page cache allocations are done from GFP_NOFS context to
+        * prevent direct reclaim recursion back into the filesystem and blowing
+        * stacks or deadlocking.
+        */
+       gfp_mask = mapping_gfp_mask(inode->i_mapping);
+       mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
+
        /*
         * If there is no attribute fork no ACL can exist on this inode,
         * and it can't have any file capabilities attached to it either.