Skip to content

Commit 2c5f4a5

Browse files
author
Andreas Gruenbacher
committed
gfs2: Prevent recursive memory reclaim
Function new_inode() returns a new inode with inode->i_mapping->gfp_mask set to GFP_HIGHUSER_MOVABLE. This value includes the __GFP_FS flag, so allocations in that address space can recurse into filesystem memory reclaim. We don't want that to happen because it can consume a significant amount of stack memory. Worse than that is that it can also deadlock: for example, in several places, gfs2_unstuff_dinode() is called inside filesystem transactions. This calls filemap_grab_folio(), which can allocate a new folio, which can trigger memory reclaim. If memory reclaim recurses into the filesystem and starts another transaction, a deadlock will ensue. To fix these kinds of problems, prevent memory reclaim from recursing into filesystem code by making sure that the gfp_mask of inode address spaces doesn't include __GFP_FS. The "meta" and resource group address spaces were already using GFP_NOFS as their gfp_mask (which doesn't include __GFP_FS). The default value of GFP_HIGHUSER_MOVABLE is less restrictive than GFP_NOFS, though. To avoid being overly limiting, use the default value and only knock off the __GFP_FS flag. I'm not sure if this will actually make a difference, but it also shouldn't hurt. This patch is loosely based on commit ad22c7a ("xfs: prevent stack overflows from page cache allocation"). Fixes xfstest generic/273. Fixes: dc0b943 ("gfs: Don't use GFP_NOFS in gfs2_unstuff_dinode") Reviewed-by: Andrew Price <anprice@redhat.com> Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
1 parent c3454ac commit 2c5f4a5

4 files changed

Lines changed: 21 additions & 2 deletions

File tree

fs/gfs2/glock.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,10 +1211,13 @@ int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
12111211

12121212
mapping = gfs2_glock2aspace(gl);
12131213
if (mapping) {
1214+
gfp_t gfp_mask;
1215+
12141216
mapping->a_ops = &gfs2_meta_aops;
12151217
mapping->host = sdp->sd_inode;
12161218
mapping->flags = 0;
1217-
mapping_set_gfp_mask(mapping, GFP_NOFS);
1219+
gfp_mask = mapping_gfp_mask(sdp->sd_inode->i_mapping);
1220+
mapping_set_gfp_mask(mapping, gfp_mask);
12181221
mapping->i_private_data = NULL;
12191222
mapping->writeback_index = 0;
12201223
}

fs/gfs2/inode.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ static int iget_set(struct inode *inode, void *opaque)
8989
return 0;
9090
}
9191

92+
void gfs2_setup_inode(struct inode *inode)
93+
{
94+
gfp_t gfp_mask;
95+
96+
/*
97+
* Ensure all page cache allocations are done from GFP_NOFS context to
98+
* prevent direct reclaim recursion back into the filesystem and blowing
99+
* stacks or deadlocking.
100+
*/
101+
gfp_mask = mapping_gfp_mask(inode->i_mapping);
102+
mapping_set_gfp_mask(inode->i_mapping, gfp_mask & ~__GFP_FS);
103+
}
104+
92105
/**
93106
* gfs2_inode_lookup - Lookup an inode
94107
* @sb: The super block
@@ -132,6 +145,7 @@ struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
132145
struct gfs2_glock *io_gl;
133146
int extra_flags = 0;
134147

148+
gfs2_setup_inode(inode);
135149
error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE,
136150
&ip->i_gl);
137151
if (unlikely(error))
@@ -752,6 +766,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
752766
error = -ENOMEM;
753767
if (!inode)
754768
goto fail_gunlock;
769+
gfs2_setup_inode(inode);
755770
ip = GFS2_I(inode);
756771

757772
error = posix_acl_create(dir, &mode, &default_acl, &acl);

fs/gfs2/inode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static inline int gfs2_check_internal_file_size(struct inode *inode,
8686
return -EIO;
8787
}
8888

89+
void gfs2_setup_inode(struct inode *inode);
8990
struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned type,
9091
u64 no_addr, u64 no_formal_ino,
9192
unsigned int blktype);

fs/gfs2/ops_fstype.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ static int gfs2_fill_super(struct super_block *sb, struct fs_context *fc)
11831183

11841184
mapping = gfs2_aspace(sdp);
11851185
mapping->a_ops = &gfs2_rgrp_aops;
1186-
mapping_set_gfp_mask(mapping, GFP_NOFS);
1186+
gfs2_setup_inode(sdp->sd_inode);
11871187

11881188
error = init_names(sdp, silent);
11891189
if (error)

0 commit comments

Comments
 (0)