Skip to content

Commit 9897713

Browse files
Michal Hockoakpm00
authored andcommitted
bcachefs: do not use PF_MEMALLOC_NORECLAIM
Patch series "remove PF_MEMALLOC_NORECLAIM" v3. This patch (of 2): bch2_new_inode relies on PF_MEMALLOC_NORECLAIM to try to allocate a new inode to achieve GFP_NOWAIT semantic while holding locks. If this allocation fails it will drop locks and use GFP_NOFS allocation context. We would like to drop PF_MEMALLOC_NORECLAIM because it is really dangerous to use if the caller doesn't control the full call chain with this flag set. E.g. if any of the function down the chain needed GFP_NOFAIL request the PF_MEMALLOC_NORECLAIM would override this and cause unexpected failure. While this is not the case in this particular case using the scoped gfp semantic is not really needed bacause we can easily pus the allocation context down the chain without too much clutter. [[email protected]: fix kerneldoc warnings] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Michal Hocko <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Dave Chinner <[email protected]> Reviewed-by: Jan Kara <[email protected]> # For vfs changes Cc: Al Viro <[email protected]> Cc: Christian Brauner <[email protected]> Cc: James Morris <[email protected]> Cc: Kent Overstreet <[email protected]> Cc: Paul Moore <[email protected]> Cc: Serge E. Hallyn <[email protected]> Cc: Yafang Shao <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Cc: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 8cf0b93 commit 9897713

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

fs/bcachefs/fs.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,10 @@ static struct inode *bch2_alloc_inode(struct super_block *sb)
300300
BUG();
301301
}
302302

303-
static struct bch_inode_info *__bch2_new_inode(struct bch_fs *c)
303+
static struct bch_inode_info *__bch2_new_inode(struct bch_fs *c, gfp_t gfp)
304304
{
305305
struct bch_inode_info *inode = alloc_inode_sb(c->vfs_sb,
306-
bch2_inode_cache, GFP_NOFS);
306+
bch2_inode_cache, gfp);
307307
if (!inode)
308308
return NULL;
309309

@@ -315,7 +315,7 @@ static struct bch_inode_info *__bch2_new_inode(struct bch_fs *c)
315315
mutex_init(&inode->ei_quota_lock);
316316
memset(&inode->ei_devs_need_flush, 0, sizeof(inode->ei_devs_need_flush));
317317

318-
if (unlikely(inode_init_always(c->vfs_sb, &inode->v))) {
318+
if (unlikely(inode_init_always_gfp(c->vfs_sb, &inode->v, gfp))) {
319319
kmem_cache_free(bch2_inode_cache, inode);
320320
return NULL;
321321
}
@@ -328,12 +328,10 @@ static struct bch_inode_info *__bch2_new_inode(struct bch_fs *c)
328328
*/
329329
static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans)
330330
{
331-
struct bch_inode_info *inode =
332-
memalloc_flags_do(PF_MEMALLOC_NORECLAIM|PF_MEMALLOC_NOWARN,
333-
__bch2_new_inode(trans->c));
331+
struct bch_inode_info *inode = __bch2_new_inode(trans->c, GFP_NOWAIT);
334332

335333
if (unlikely(!inode)) {
336-
int ret = drop_locks_do(trans, (inode = __bch2_new_inode(trans->c)) ? 0 : -ENOMEM);
334+
int ret = drop_locks_do(trans, (inode = __bch2_new_inode(trans->c, GFP_NOFS)) ? 0 : -ENOMEM);
337335
if (ret && inode) {
338336
__destroy_inode(&inode->v);
339337
kmem_cache_free(bch2_inode_cache, inode);
@@ -407,7 +405,7 @@ __bch2_create(struct mnt_idmap *idmap,
407405
if (ret)
408406
return ERR_PTR(ret);
409407
#endif
410-
inode = __bch2_new_inode(c);
408+
inode = __bch2_new_inode(c, GFP_NOFS);
411409
if (unlikely(!inode)) {
412410
inode = ERR_PTR(-ENOMEM);
413411
goto err;

fs/inode.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,16 @@ static int no_open(struct inode *inode, struct file *file)
146146
}
147147

148148
/**
149-
* inode_init_always - perform inode structure initialisation
149+
* inode_init_always_gfp - perform inode structure initialisation
150150
* @sb: superblock inode belongs to
151151
* @inode: inode to initialise
152+
* @gfp: allocation flags
152153
*
153154
* These are initializations that need to be done on every inode
154155
* allocation as the fields are not initialised by slab allocation.
156+
* If there are additional allocations required @gfp is used.
155157
*/
156-
int inode_init_always(struct super_block *sb, struct inode *inode)
158+
int inode_init_always_gfp(struct super_block *sb, struct inode *inode, gfp_t gfp)
157159
{
158160
static const struct inode_operations empty_iops;
159161
static const struct file_operations no_open_fops = {.open = no_open};
@@ -230,14 +232,14 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
230232
#endif
231233
inode->i_flctx = NULL;
232234

233-
if (unlikely(security_inode_alloc(inode)))
235+
if (unlikely(security_inode_alloc(inode, gfp)))
234236
return -ENOMEM;
235237

236238
this_cpu_inc(nr_inodes);
237239

238240
return 0;
239241
}
240-
EXPORT_SYMBOL(inode_init_always);
242+
EXPORT_SYMBOL(inode_init_always_gfp);
241243

242244
void free_inode_nonrcu(struct inode *inode)
243245
{

include/linux/fs.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3082,7 +3082,12 @@ extern loff_t default_llseek(struct file *file, loff_t offset, int whence);
30823082

30833083
extern loff_t vfs_llseek(struct file *file, loff_t offset, int whence);
30843084

3085-
extern int inode_init_always(struct super_block *, struct inode *);
3085+
extern int inode_init_always_gfp(struct super_block *, struct inode *, gfp_t);
3086+
static inline int inode_init_always(struct super_block *sb, struct inode *inode)
3087+
{
3088+
return inode_init_always_gfp(sb, inode, GFP_NOFS);
3089+
}
3090+
30863091
extern void inode_init_once(struct inode *);
30873092
extern void address_space_init_once(struct address_space *mapping);
30883093
extern struct inode * igrab(struct inode *);

include/linux/security.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ int security_dentry_create_files_as(struct dentry *dentry, int mode,
348348
struct cred *new);
349349
int security_path_notify(const struct path *path, u64 mask,
350350
unsigned int obj_type);
351-
int security_inode_alloc(struct inode *inode);
351+
int security_inode_alloc(struct inode *inode, gfp_t gfp);
352352
void security_inode_free(struct inode *inode);
353353
int security_inode_init_security(struct inode *inode, struct inode *dir,
354354
const struct qstr *qstr,
@@ -789,7 +789,7 @@ static inline int security_path_notify(const struct path *path, u64 mask,
789789
return 0;
790790
}
791791

792-
static inline int security_inode_alloc(struct inode *inode)
792+
static inline int security_inode_alloc(struct inode *inode, gfp_t gfp)
793793
{
794794
return 0;
795795
}

security/security.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,19 +740,20 @@ static int lsm_file_alloc(struct file *file)
740740
/**
741741
* lsm_inode_alloc - allocate a composite inode blob
742742
* @inode: the inode that needs a blob
743+
* @gfp: allocation flags
743744
*
744745
* Allocate the inode blob for all the modules
745746
*
746747
* Returns 0, or -ENOMEM if memory can't be allocated.
747748
*/
748-
static int lsm_inode_alloc(struct inode *inode)
749+
static int lsm_inode_alloc(struct inode *inode, gfp_t gfp)
749750
{
750751
if (!lsm_inode_cache) {
751752
inode->i_security = NULL;
752753
return 0;
753754
}
754755

755-
inode->i_security = kmem_cache_zalloc(lsm_inode_cache, GFP_NOFS);
756+
inode->i_security = kmem_cache_zalloc(lsm_inode_cache, gfp);
756757
if (inode->i_security == NULL)
757758
return -ENOMEM;
758759
return 0;
@@ -1678,16 +1679,17 @@ int security_path_notify(const struct path *path, u64 mask,
16781679
/**
16791680
* security_inode_alloc() - Allocate an inode LSM blob
16801681
* @inode: the inode
1682+
* @gfp: allocation flags
16811683
*
16821684
* Allocate and attach a security structure to @inode->i_security. The
16831685
* i_security field is initialized to NULL when the inode structure is
16841686
* allocated.
16851687
*
16861688
* Return: Return 0 if operation was successful.
16871689
*/
1688-
int security_inode_alloc(struct inode *inode)
1690+
int security_inode_alloc(struct inode *inode, gfp_t gfp)
16891691
{
1690-
int rc = lsm_inode_alloc(inode);
1692+
int rc = lsm_inode_alloc(inode, gfp);
16911693

16921694
if (unlikely(rc))
16931695
return rc;

0 commit comments

Comments
 (0)