Remove FGP_NOFS
Subsystems (9)
Matthew Wilcox (Oracle)
maintainercover Remove FGP_NOFS
There is broad agreement that we should eliminate __GFP_FS in favour of using memalloc_nofs_save(). This is a small step in that direction by getting rid of FGP_NOFS. It's also a nice simplification in its own right.
I added the documentation improvement at the start of this series, not because anything depends on that (obviously it doesn't), but because I thought the existing documentation was rather MM centric and it needs to be FS centric. Hopefully the cc'd filesystem maintainers will have the chance to look at patch 1 and see if it makes sense to them.
There's a few different solutions here:
- Filesystems specifying both FGP_NOWAIT and FGP_NOFS. If you're not waiting, it doesn't matter if you're allowed to reclaim from FS context or not, you're just not doing reclaim.
- We're already under memalloc_nofs.
- We can prove that the MM should be allowed to reclaim from filesystem memory (and so specifying FGP_NOFS was wrong).
- We can add a call to memalloc_nofs_save().
Matthew Wilcox (Oracle) (9):
mm: Improve memalloc_nofs_save() documentation
ext4: Remove uses of FGP_NOFS
erofs: Remove redundant FGP_NOFS
nfs: Remove redundant FGP_NOFS
ntfs: Remove FGP_NOFS from __ntfs_inode_resident_attr_pwrite()
ntfs: Remove redundant FGP_NOFS from ntfs_wof_collect_dest()
squashfs: Remove use of FGP_NOFS
filemap: Remove redundant FGP_NOFS
filemap: Remove FGP_NOFS
fs/erofs/internal.h | 2 +- fs/ext4/inline.c | 6 ++---- fs/nfs/dir.c | 2 +- fs/ntfs/inode.c | 2 +- fs/ntfs/wof.c | 7 +++---- fs/squashfs/file.c | 5 ++++- include/linux/pagemap.h | 12 +++++------- include/linux/sched/mm.h | 31 +++++++++++++++++++++++-------- mm/filemap.c | 2 -- mm/readahead.c | 14 ++------------ 10 files changed, 42 insertions(+), 41 deletions(-)
Signature · 1 line
2.47.3
Matthew Wilcox (Oracle)
maintainerPATCH 1/9 mm: Improve memalloc_nofs_save() documentation
Talk about why someone should call this function instead of what this function does. This makes the long comment in mm/readahead.c obsolete; replace it with a comment about which case we're protecting against.
--- include/linux/sched/mm.h | 31 +++++++++++++++++++++++-------- mm/readahead.c | 14 ++------------ 2 files changed, 25 insertions(+), 20 deletions(-)
include/linux/sched/mm.h +23 -8
@@ -372,13 +372,26 @@ static inline void memalloc_noio_restore(unsigned int flags) }/** -* memalloc_nofs_save - Marks implicit GFP_NOFS allocation scope. +* memalloc_nofs_save - Prevent recursion into the filesystem. * -* This functions marks the beginning of the GFP_NOFS allocation scope. -* All further allocations will implicitly drop __GFP_FS flag and so -* they are safe for the FS critical section from the allocation recursion -* point of view. Use memalloc_nofs_restore to end the scope with flags -* returned by this function. +* All memory allocations between calling this function and calling +* memalloc_nofs_restore() will be prevented from calling into filesystems +* to reclaim memory. Clean page cache memory can still be reclaimed, +* but (for example) inodes will not be. +* +* The primary reason to do this is that the caller has taken a lock +* which would be needed by FS reclaim. While we could theoretically +* call into a different filesystem in this case, it can be a deep call +* stack so it is better to avoid all filesystems. +* +* Filesystems often choose to incorporate a call to this function as part +* of starting a journal transaction. While not a lock in the normal +* sense, it has much the same effect as nested journal transactions +* are either prohibited or expensive. +* +* Also call this function if you need to allocate memory while holding +* a file folio locked. High order allocations (such as those requested +* by slab) can trigger compaction which will attempt to lock the folio. * * Context: This function is safe to be used from any context. * Return: The saved flags to be passed to memalloc_nofs_restore.
@@ -389,10 +402,12 @@ static inline unsigned int memalloc_nofs_save(void) }/** -* memalloc_nofs_restore - Ends the implicit GFP_NOFS scope. +* memalloc_nofs_restore - End filesystem reclaim scope. * @flags: Flags to restore. * -* Ends the implicit GFP_NOFS scope started by memalloc_nofs_save function. +* Ends the implicit memory allocation scope started by +* memalloc_nofs_save(). This may not enable access to filesystem reclaim +* if it was already disabled at the time memalloc_nofs_save() was called. * Always make sure that the given flags is the return value from the * pairing memalloc_nofs_save call. */
mm/readahead.c +2 -12
@@ -227,17 +227,7 @@ void page_cache_ra_unbounded(struct readahead_control *ractl,gfp_t gfp_mask =readahead_gfp_mask (mapping);unsigned long mark =ULONG_MAX , i =0 ;unsigned int min_nrpages =mapping_min_folio_nrpages (mapping); - -/* -* Partway through the readahead operation, we will have added -* locked pages to the page cache, but will not yet have submitted -* them for I/O. Adding another page may need to allocate memory, -* which can trigger memory reclaim. Telling the VM we're in -* the middle of a filesystem operation will cause it to not -* touch file-backed pages, preventing a deadlock. Most (all?) -* filesystems already specify __GFP_NOFS in their mapping's -* gfp_mask, but let's be explicit here. -*/ +/* Allocating with locked folios */ unsigned int nofs =memalloc_nofs_save ();lockdep_assert_held (&mapping->invalidate_lock);
@@ -512,7 +502,7 @@ void page_cache_ra_order(struct readahead_control *ractl, ra->order = new_order; -/* See comment in page_cache_ra_unbounded() */ +/* Allocating with locked folios */ nofs =memalloc_nofs_save ();filemap_invalidate_lock_shared (mapping);/*
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 1/9] mm: Improve memalloc_nofs_save() documentation
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
Talk about why someone should call this function instead of what this function does. This makes the long comment in
mm/readahead.cobsolete; replace it with a comment about which case we're protecting against.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH 1/9] mm: Improve memalloc_nofs_save() documentation
In reply to Matthew Wilcox (Oracle)Quoted 6 lines · Matthew Wilcox (Oracle)
Talk about why someone should call this function instead of what this function does. This makes the long comment in
mm/readahead.cobsolete; replace it with a comment about which case we're protecting against.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Yeah, I think the documentation is better. Feel free to add:
Honza
Quoted 92 lines
--- include/linux/sched/mm.h | 31 +++++++++++++++++++++++-------- mm/readahead.c | 14 ++------------ 2 files changed, 25 insertions(+), 20 deletions(-)include/linux/sched/mm.h +23
-8@@ -372,13 +372,26 @@ static inline void memalloc_noio_restore(unsigned int flags) }/** -* memalloc_nofs_save - Marks implicit GFP_NOFS allocation scope. +* memalloc_nofs_save - Prevent recursion into the filesystem. * -* This functions marks the beginning of the GFP_NOFS allocation scope. -* All further allocations will implicitly drop __GFP_FS flag and so -* they are safe for the FS critical section from the allocation recursion -* point of view. Use memalloc_nofs_restore to end the scope with flags -* returned by this function. +* All memory allocations between calling this function and calling +* memalloc_nofs_restore() will be prevented from calling into filesystems +* to reclaim memory. Clean page cache memory can still be reclaimed, +* but (for example) inodes will not be. +* +* The primary reason to do this is that the caller has taken a lock +* which would be needed by FS reclaim. While we could theoretically +* call into a different filesystem in this case, it can be a deep call +* stack so it is better to avoid all filesystems. +* +* Filesystems often choose to incorporate a call to this function as part +* of starting a journal transaction. While not a lock in the normal +* sense, it has much the same effect as nested journal transactions +* are either prohibited or expensive. +* +* Also call this function if you need to allocate memory while holding +* a file folio locked. High order allocations (such as those requested +* by slab) can trigger compaction which will attempt to lock the folio. * * Context: This function is safe to be used from any context. * Return: The saved flags to be passed to memalloc_nofs_restore. @@ -389,10 +402,12 @@ static inline unsigned int memalloc_nofs_save(void) }/** -* memalloc_nofs_restore - Ends the implicit GFP_NOFS scope. +* memalloc_nofs_restore - End filesystem reclaim scope. * @flags: Flags to restore. * -* Ends the implicit GFP_NOFS scope started by memalloc_nofs_save function. +* Ends the implicit memory allocation scope started by +* memalloc_nofs_save(). This may not enable access to filesystem reclaim +* if it was already disabled at the time memalloc_nofs_save() was called. * Always make sure that the given flags is the return value from the * pairing memalloc_nofs_save call. */ mm/readahead.c +2
-12@@ -227,17 +227,7 @@ void page_cache_ra_unbounded(struct readahead_control *ractl,gfp_t gfp_mask =readahead_gfp_mask (mapping);unsigned long mark =ULONG_MAX , i =0 ;unsigned int min_nrpages =mapping_min_folio_nrpages (mapping); - -/* -* Partway through the readahead operation, we will have added -* locked pages to the page cache, but will not yet have submitted -* them for I/O. Adding another page may need to allocate memory, -* which can trigger memory reclaim. Telling the VM we're in -* the middle of a filesystem operation will cause it to not -* touch file-backed pages, preventing a deadlock. Most (all?) -* filesystems already specify __GFP_NOFS in their mapping's -* gfp_mask, but let's be explicit here. -*/ +/* Allocating with locked folios */ unsigned int nofs =memalloc_nofs_save ();lockdep_assert_held (&mapping->invalidate_lock);@@ -512,7 +502,7 @@ void page_cache_ra_order(struct readahead_control *ractl, ra->order = new_order; -/* See comment in page_cache_ra_unbounded() */ +/* Allocating with locked folios */ nofs =memalloc_nofs_save ();filemap_invalidate_lock_shared (mapping);/* Signature · 1 line
2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Matthew Wilcox (Oracle)
maintainerPATCH 2/9 ext4: Remove uses of FGP_NOFS
In reply to Matthew Wilcox (Oracle)Both of these locations are called while a transaction is in progress. Starting a transaction uses memalloc_nofs_save(), so we are already prevented from recursing into the filesystem and using GFP_NOFS was unnecessary.
--- fs/ext4/inline.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
fs/ext4/inline.c +2 -4
@@ -601,9 +601,7 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping, goto out; } -/* We cannot recurse into the filesystem as the transaction is already -* started */ - folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN |FGP_NOFS , + folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN ,mapping_gfp_mask (mapping));if (IS_ERR (folio)) { ret =PTR_ERR (folio);
@@ -734,7 +732,7 @@ int ext4_generic_write_inline_data(struct address_space *mapping, goto out_release_bh; } - folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN |FGP_NOFS , + folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN ,mapping_gfp_mask (mapping));if (IS_ERR (folio)) { ret =PTR_ERR (folio);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 2/9] ext4: Remove uses of FGP_NOFS
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
Both of these locations are called while a transaction is in progress. Starting a transaction uses
memalloc_nofs_save(), so we are already prevented from recursing into the filesystem and usingGFP_NOFSwas unnecessary.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/9] ext4: Remove uses of FGP_NOFS
In reply to Matthew Wilcox (Oracle)Quoted 6 lines · Matthew Wilcox (Oracle)
Both of these locations are called while a transaction is in progress. Starting a transaction uses
memalloc_nofs_save(), so we are already prevented from recursing into the filesystem and usingGFP_NOFSwas unnecessary.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 31 lines
--- fs/ext4/inline.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)fs/ext4/inline.c +2
-4@@ -601,9 +601,7 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping, goto out; } -/* We cannot recurse into the filesystem as the transaction is already -* started */ - folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN |FGP_NOFS , + folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN ,mapping_gfp_mask (mapping));if (IS_ERR (folio)) { ret =PTR_ERR (folio);@@ -734,7 +732,7 @@ int ext4_generic_write_inline_data(struct address_space *mapping, goto out_release_bh; } - folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN |FGP_NOFS , + folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN ,mapping_gfp_mask (mapping));if (IS_ERR (folio)) { ret =PTR_ERR (folio);Signature · 1 line
2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 2/9] ext4: Remove uses of FGP_NOFS
In reply to Matthew Wilcox (Oracle)Quoted 6 lines · Matthew Wilcox (Oracle)
Both of these locations are called while a transaction is in progress. Starting a transaction uses
memalloc_nofs_save(), so we are already prevented from recursing into the filesystem and usingGFP_NOFSwas unnecessary.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Looks good to me.
Quoted 28 lines
--- fs/ext4/inline.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)fs/ext4/inline.c +2
-4@@ -601,9 +601,7 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping, goto out; } -/* We cannot recurse into the filesystem as the transaction is already -* started */ - folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN |FGP_NOFS , + folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN ,mapping_gfp_mask (mapping));if (IS_ERR (folio)) { ret =PTR_ERR (folio);@@ -734,7 +732,7 @@ int ext4_generic_write_inline_data(struct address_space *mapping, goto out_release_bh; } - folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN |FGP_NOFS , + folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN ,mapping_gfp_mask (mapping));if (IS_ERR (folio)) { ret =PTR_ERR (folio);
Re: [PATCH 2/9] ext4: Remove uses of FGP_NOFS
In reply to Matthew Wilcox (Oracle)Quoted 6 lines · Matthew Wilcox (Oracle)
Both of these locations are called while a transaction is in progress. Starting a transaction uses
memalloc_nofs_save(), so we are already prevented from recursing into the filesystem and usingGFP_NOFSwas unnecessary.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Looks good to me.
Quoted 28 lines
--- fs/ext4/inline.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)fs/ext4/inline.c +2
-4@@ -601,9 +601,7 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping, goto out; } -/* We cannot recurse into the filesystem as the transaction is already -* started */ - folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN |FGP_NOFS , + folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN ,mapping_gfp_mask (mapping));if (IS_ERR (folio)) { ret =PTR_ERR (folio);@@ -734,7 +732,7 @@ int ext4_generic_write_inline_data(struct address_space *mapping, goto out_release_bh; } - folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN |FGP_NOFS , + folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN ,mapping_gfp_mask (mapping));if (IS_ERR (folio)) { ret =PTR_ERR (folio);
Re: [PATCH 2/9] ext4: Remove uses of FGP_NOFS
In reply to Matthew Wilcox (Oracle)Quoted 6 lines · Matthew Wilcox (Oracle)
Both of these locations are called while a transaction is in progress. Starting a transaction uses
memalloc_nofs_save(), so we are already prevented from recursing into the filesystem and usingGFP_NOFSwas unnecessary.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Matthew Wilcox (Oracle)
maintainerPATCH 3/9 erofs: Remove redundant FGP_NOFS
In reply to Matthew Wilcox (Oracle)The flags already include FGP_NOWAIT which prohibits all reclaim, not just reclaiming from the filesystem.
--- fs/erofs/internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/erofs/internal.h +1 -1
@@ -327,7 +327,7 @@ static inline struct folio *erofs_grab_folio_nowait(struct address_space *as, pgoff_t index) {return __filemap_get_folio (as, index, -FGP_LOCK |FGP_CREAT |FGP_NOFS |FGP_NOWAIT , +FGP_LOCK |FGP_CREAT |FGP_NOWAIT ,readahead_gfp_mask (as) & ~__GFP_RECLAIM); }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 3/9] erofs: Remove redundant FGP_NOFS
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines
The flags already include
FGP_NOWAITwhich prohibits all reclaim, not just reclaiming from the filesystem.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 3/9] erofs: Remove redundant FGP_NOFS
In reply to Matthew Wilcox (Oracle)Quoted 4 lines · Matthew Wilcox (Oracle)
The flags already include
FGP_NOWAITwhich prohibits all reclaim, not just reclaiming from the filesystem.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 20 lines
--- fs/erofs/internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/erofs/internal.h +1
-1@@ -327,7 +327,7 @@ static inline struct folio *erofs_grab_folio_nowait(struct address_space *as, pgoff_t index) {return __filemap_get_folio (as, index, -FGP_LOCK |FGP_CREAT |FGP_NOFS |FGP_NOWAIT , +FGP_LOCK |FGP_CREAT |FGP_NOWAIT ,readahead_gfp_mask (as) & ~__GFP_RECLAIM); }Signature · 1 line
2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 3/9] erofs: Remove redundant FGP_NOFS
In reply to Matthew Wilcox (Oracle)Quoted 4 lines · Matthew Wilcox (Oracle)
The flags already include
FGP_NOWAITwhich prohibits all reclaim, not just reclaiming from the filesystem.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Thanks,
Gao Xiang
Matthew Wilcox (Oracle)
maintainerPATCH 4/9 nfs: Remove redundant FGP_NOFS
In reply to Matthew Wilcox (Oracle)The flags already include FGP_NOWAIT which prohibits all reclaim, not just reclaiming from the filesystem.
--- fs/nfs/dir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfs/dir.c +1 -1
@@ -447,7 +447,7 @@ static struct folio *nfs_readdir_folio_get_next(struct address_space *mapping,struct folio *folio; folio =__filemap_get_folio (mapping, index, -FGP_LOCK |FGP_CREAT |FGP_NOFS |FGP_NOWAIT , +FGP_LOCK |FGP_CREAT |FGP_NOWAIT ,mapping_gfp_mask (mapping));if (IS_ERR (folio))return NULL ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 4/9] nfs: Remove redundant FGP_NOFS
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines
The flags already include
FGP_NOWAITwhich prohibits all reclaim, not just reclaiming from the filesystem.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH 4/9] nfs: Remove redundant FGP_NOFS
In reply to Matthew Wilcox (Oracle)Quoted 4 lines · Matthew Wilcox (Oracle)
The flags already include
FGP_NOWAITwhich prohibits all reclaim, not just reclaiming from the filesystem.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Obvious enough :) feel free to add:
Honza
Quoted 20 lines
--- fs/nfs/dir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/nfs/dir.c +1
-1@@ -447,7 +447,7 @@ static struct folio *nfs_readdir_folio_get_next(struct address_space *mapping,struct folio *folio; folio =__filemap_get_folio (mapping, index, -FGP_LOCK |FGP_CREAT |FGP_NOFS |FGP_NOWAIT , +FGP_LOCK |FGP_CREAT |FGP_NOWAIT ,mapping_gfp_mask (mapping));if (IS_ERR (folio))return NULL ;Signature · 1 line
2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Matthew Wilcox (Oracle)
maintainerPATCH 5/9 ntfs: Remove FGP_NOFS from __ntfs_inode_resident_attr_pwrite()
In reply to Matthew Wilcox (Oracle)I do not believe that FGP_NOFS is needed here. This function is called from one place (ntfs_inode_attr_pwrite) which calls either this function or __ntfs_inode_non_resident_attr_pwrite(). The latter function does not use FGP_NOFS in its calls to allocate pagecaache memory, so it should not be needed here either. If it turns out to be needed by both functions, then we should use memalloc_nofs_save at a higher level to protect both of them.
--- fs/ntfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ntfs/inode.c +1 -1
@@ -3676,7 +3676,7 @@ static s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi,mark_mft_record_dirty (ctx->ntfs_ino);/* Keep the first page clean and uptodate */ - folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN |FGP_NOFS , + folio =__filemap_get_folio (mapping,0 ,FGP_WRITEBEGIN ,mapping_gfp_mask (mapping));if (IS_ERR (folio)) { err =PTR_ERR (folio);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 5/9] ntfs: Remove FGP_NOFS from __ntfs_inode_resident_attr_pwrite()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Deadlock due to missing NOFS protection when allocating page cache memory under mrec_lock --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Deadlock due to missing NOFS protection when allocating page cache memory under mrec_lock --
commit cf448ed1a05ff7894f41c5a1a1f1f6cc405004f7
Author: Matthew Wilcox (Oracle) <[email protected]>
ntfs: Remove FGP_NOFS from __ntfs_inode_resident_attr_pwrite()
This commit removes the FGP_NOFS flag from the folio allocation in __ntfs_inode_resident_attr_pwrite(). The commit message notes that if the protection is needed, it should be done using memalloc_nofs_save() at a higher level.
fs/ntfs/inode.c static s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi, · line 3679
Quoted 10 lines
fs/ntfs/inode.c +1
-1@@ -3676,7 +3676,7 @@ static s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi,mark_mft_record_dirty (ctx->ntfs_ino);/* Keep the first page clean and uptodate */ - folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS, + folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN,
[Severity: High]
Does removing FGP_NOFS here without adding the higher-level memalloc_nofs_save() protection introduce a deadlock with flusher threads?
If a user writes a resident extended attribute via ntfs_setxattr(), the call path acquires mrec_lock before reaching this allocation:
fs/ntfs/ea.c:ntfs_setxattr() {
...
mutex_lock(&ni->mrec_lock);
err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, NULL);
mutex_unlock(&ni->mrec_lock);
...
}Without FGP_NOFS, the folio allocation can now occur with __GFP_FS enabled. If the system is under memory pressure and blocks to wait for flusher threads, those threads may attempt to write back pages for this NTFS filesystem. The writeback path also unconditionally acquires the same mrec_lock:
fs/ntfs/iomap.c:ntfs_write_iomap_begin_non_resident() {
...
mutex_lock(&NTFS_I(inode)->mrec_lock);
...
}Can this cause a cyclic deadlock on mrec_lock if the flusher thread blocks waiting on the locked inode?
Quoted 3 lines
fs/ntfs/inode.c +0
-0mapping_gfp_mask(mapping));if (IS_ERR (folio)) { err =PTR_ERR (folio);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH 5/9] ntfs: Remove FGP_NOFS from __ntfs_inode_resident_attr_pwrite()
In reply to Matthew Wilcox (Oracle)Quoted 10 lines · Matthew Wilcox (Oracle)
I do not believe that
FGP_NOFSis needed here. This function is called from one place (ntfs_inode_attr_pwrite) which calls either this function or__ntfs_inode_non_resident_attr_pwrite(). The latter function does not useFGP_NOFSin its calls to allocate pagecaache memory, so it should not be needed here either. If it turns out to be needed by both functions, then we should usememalloc_nofs_saveat a higher level to protect both of them.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Thanks!
Matthew Wilcox (Oracle)
maintainerPATCH 6/9 ntfs: Remove redundant FGP_NOFS from ntfs_wof_collect_dest()
In reply to Matthew Wilcox (Oracle)The flags already include FGP_NOWAIT which prohibits all reclaim, not just reclaiming from the filesystem. Use mapping_gfp_mask() instead of GFP_NOFS as this will preserve flags like GFP_MOVABLE.
--- fs/ntfs/wof.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
fs/ntfs/wof.c +3 -4
@@ -464,10 +464,9 @@ static int ntfs_wof_collect_dest(struct address_space *mapping, folio = target; is_target = true; }else { - folio =__filemap_get_folio ( - mapping, index, -FGP_LOCK |FGP_CREAT |FGP_NOFS |FGP_NOWAIT , -GFP_NOFS ); + folio =__filemap_get_folio (mapping, index, +FGP_LOCK |FGP_CREAT |FGP_NOWAIT , +mapping_gfp_mask (mapping));if (IS_ERR (folio))return PTR_ERR (folio); is_target = false;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 6/9] ntfs: Remove redundant FGP_NOFS from ntfs_wof_collect_dest()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines
The flags already include
FGP_NOWAITwhich prohibits all reclaim, not just reclaiming from the filesystem. Usemapping_gfp_mask()instead ofGFP_NOFSas this will preserve flags likeGFP_MOVABLE.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH 6/9] ntfs: Remove redundant FGP_NOFS from ntfs_wof_collect_dest()
In reply to Matthew Wilcox (Oracle)Quoted 6 lines · Matthew Wilcox (Oracle)
The flags already include
FGP_NOWAITwhich prohibits all reclaim, not just reclaiming from the filesystem. Usemapping_gfp_mask()instead ofGFP_NOFSas this will preserve flags likeGFP_MOVABLE.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Thanks!
Matthew Wilcox (Oracle)
maintainerPATCH 7/9 squashfs: Remove use of FGP_NOFS
In reply to Matthew Wilcox (Oracle)Since we can allocate memory while holding a folio locked, we cannot allow page reclaim to call into the filesystem and attempt to compact the folio we're currently holding locked. Use memalloc_nofs_save() instead of FGP_NOFS here.
--- fs/squashfs/file.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fs/squashfs/file.c +4 -1
@@ -398,6 +398,7 @@ void squashfs_copy_cache(struct folio *folio,struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;int i, mask = (1 << (msblk->block_log -PAGE_SHIFT )) -1 ;int start_index = folio->index & ~mask, end_index = start_index | mask; +unsigned int nofs =memalloc_nofs_save ();/* * Loop copying datablock into pages. As the datablock likely covers
@@ -415,7 +416,7 @@ void squashfs_copy_cache(struct folio *folio, push_folio = (i == folio->index) ? folio :__filemap_get_folio (mapping, i, -FGP_LOCK |FGP_CREAT |FGP_NOFS |FGP_NOWAIT , +FGP_LOCK |FGP_CREAT |FGP_NOWAIT ,mapping_gfp_mask (mapping));if (IS_ERR (push_folio))
@@ -430,6 +431,8 @@ void squashfs_copy_cache(struct folio *folio,if (i != folio->index)folio_put (push_folio); } + +memalloc_nofs_restore (nofs); }/* Read datablock stored packed inside a fragment (tail-end packed block) */
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 7/9] squashfs: Remove use of FGP_NOFS
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Unprotected GFP_KERNEL allocations while holding folio locks can cause compaction deadlocks. --
commit f9b201cd220cb4651ba3f51b039ab61732317528
Author: Matthew Wilcox (Oracle) <[email protected]>
squashfs: Remove use of FGP_NOFS
This commit replaces the use of FGP_NOFS with memalloc_nofs_save() in squashfs_copy_cache() to prevent page reclaim from calling into the filesystem and attempting to compact the locked folio.
Quoted 4 lines
fs/squashfs/file.c +0
-0
[ ... ]
fs/squashfs/file.c void squashfs_copy_cache(struct folio *folio, · line 404
Quoted 8 lines
fs/squashfs/file.c +1
-0@@ -398,6 +398,7 @@ void squashfs_copy_cache(struct folio *folio,struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;int i, mask = (1 << (msblk->block_log -PAGE_SHIFT )) -1 ;int start_index = folio->index & ~mask, end_index = start_index | mask; +unsigned int nofs =memalloc_nofs_save ();/* * Loop copying datablock into pages. As the datablock likely covers
[Severity: High]
This is a pre-existing issue, but while this patch fixes the allocation context in squashfs_copy_cache(), does the same compaction deadlock risk apply to other GFP_KERNEL allocations in the read_folio path?
For example, in fs/squashfs/file.c:read_indexes():
__le32 *blist = kmalloc(PAGE_SIZE, GFP_KERNEL);
And in fs/squashfs/file_direct.c:squashfs_readpage_block():
page = kmalloc_array(pages, sizeof(void *), GFP_KERNEL);
Both of these allocations occur deep within the read_folio call chain while the target folio is held locked by the VFS. If memory pressure triggers direct reclaim and synchronous compaction, it could attempt to lock the exact same folio that the thread is already holding, leading to a deadlock.
While VFS readahead callers establish a NOFS context via memalloc_nofs_save(), read_folio callers like filemap_read_folio() do not. Should these locations also be updated to prevent compaction deadlocks?
Quoted 7 lines
fs/squashfs/file.c +2
-0@@ -430,6 +431,8 @@ void squashfs_copy_cache(struct folio *folio,if (i != folio->index)folio_put (push_folio); } + +memalloc_nofs_restore (nofs); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Jan Kara
maintainerRe: [PATCH 7/9] squashfs: Remove use of FGP_NOFS
In reply to Matthew Wilcox (Oracle)Quoted 15 lines · Matthew Wilcox (Oracle)
Since we can allocate memory while holding a folio locked, we cannot allow page reclaim to call into the filesystem and attempt to compact the folio we're currently holding locked. Use
memalloc_nofs_save()instead ofFGP_NOFShere.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>--- fs/squashfs/file.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)fs/squashfs/file.c +0
-0
fs/squashfs/file.c void squashfs_copy_cache(struct folio *folio, · line 401
Quoted 4 lines
fs/squashfs/file.c +1
-0@@ -398,6 +398,7 @@ void squashfs_copy_cache(struct folio *folio,struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;int i, mask = (1 << (msblk->block_log -PAGE_SHIFT )) -1 ;int start_index = folio->index & ~mask, end_index = start_index | mask; +unsigned int nofs =memalloc_nofs_save ();
Why do you need this? The only allocation I can see is the one using FGP_NOWAIT below...
Honza
Quoted 24 lines
fs/squashfs/file.c +3
-1/* * Loop copying datablock into pages. As the datablock likely covers @@ -415,7 +416,7 @@ void squashfs_copy_cache(struct folio *folio, push_folio = (i == folio->index) ? folio :__filemap_get_folio (mapping, i, -FGP_LOCK |FGP_CREAT |FGP_NOFS |FGP_NOWAIT , +FGP_LOCK |FGP_CREAT |FGP_NOWAIT ,mapping_gfp_mask (mapping));if (IS_ERR (push_folio))@@ -430,6 +431,8 @@ void squashfs_copy_cache(struct folio *folio,if (i != folio->index)folio_put (push_folio); } + +memalloc_nofs_restore (nofs); }/* Read datablock stored packed inside a fragment (tail-end packed block) */ Signature · 1 line
2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Matthew Wilcox (Oracle)
maintainerPATCH 8/9 filemap: Remove redundant FGP_NOFS
In reply to Matthew Wilcox (Oracle)The flags already include FGP_NOWAIT which prohibits all reclaim, not just reclaiming from the filesystem.
--- include/linux/pagemap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/pagemap.h +1 -1
@@ -929,7 +929,7 @@ static inline struct page *grab_cache_page_nowait(struct address_space *mapping, pgoff_t index) {return pagecache_get_page (mapping, index, -FGP_LOCK |FGP_CREAT |FGP_NOFS |FGP_NOWAIT , +FGP_LOCK |FGP_CREAT |FGP_NOWAIT ,mapping_gfp_mask (mapping)); }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 8/9] filemap: Remove redundant FGP_NOFS
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines
The flags already include
FGP_NOWAITwhich prohibits all reclaim, not just reclaiming from the filesystem.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH 8/9] filemap: Remove redundant FGP_NOFS
In reply to Matthew Wilcox (Oracle)Quoted 4 lines · Matthew Wilcox (Oracle)
The flags already include
FGP_NOWAITwhich prohibits all reclaim, not just reclaiming from the filesystem.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 20 lines
--- include/linux/pagemap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/pagemap.h +1
-1@@ -929,7 +929,7 @@ static inline struct page *grab_cache_page_nowait(struct address_space *mapping, pgoff_t index) {return pagecache_get_page (mapping, index, -FGP_LOCK |FGP_CREAT |FGP_NOFS |FGP_NOWAIT , +FGP_LOCK |FGP_CREAT |FGP_NOWAIT ,mapping_gfp_mask (mapping)); }Signature · 1 line
2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Matthew Wilcox (Oracle)
maintainerPATCH 9/9 filemap: Remove FGP_NOFS
In reply to Matthew Wilcox (Oracle)There are no more users of FGP_NOFS. Remove it.
--- include/linux/pagemap.h | 10 ++++------ mm/filemap.c | 2 -- 2 files changed, 4 insertions(+), 8 deletions(-)
include/linux/pagemap.h +4 -6
@@ -697,7 +697,6 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping,* folio is already in cache. If the folio was allocated, unlock it * before returning so the caller can do the same dance. * * %FGP_WRITE - The folio will be written to by the caller. -* * %FGP_NOFS - __GFP_FS will get cleared in gfp. * * %FGP_NOWAIT - Don't block on the folio lock. * * %FGP_STABLE - Wait for the folio to be stable (finished writeback) * * %FGP_DONTCACHE - Uncached buffered IO
@@ -710,11 +709,10 @@ typedef unsigned int __bitwise fgf_t;#define FGP_LOCK ((__force fgf_t)0x00000002)#define FGP_CREAT ((__force fgf_t)0x00000004)#define FGP_WRITE ((__force fgf_t)0x00000008) -#define FGP_NOFS ((__force fgf_t)0x00000010) -#define FGP_NOWAIT ((__force fgf_t)0x00000020) -#define FGP_FOR_MMAP ((__force fgf_t)0x00000040) -#define FGP_STABLE ((__force fgf_t)0x00000080) -#define FGP_DONTCACHE ((__force fgf_t)0x00000100) +#define FGP_NOWAIT ((__force fgf_t)0x00000010) +#define FGP_FOR_MMAP ((__force fgf_t)0x00000020) +#define FGP_STABLE ((__force fgf_t)0x00000040) +#define FGP_DONTCACHE ((__force fgf_t)0x00000080)#define FGF_GET_ORDER (fgf) (((__force unsigned)fgf) >> 26)/* top 6 bits */ #define FGP_WRITEBEGIN (FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE)
mm/filemap.c +0 -2
@@ -1994,8 +1994,6 @@ struct folio *__filemap_get_folio_mpol(struct address_space *mapping,if ((fgp_flags &FGP_WRITE ) &&mapping_can_writeback (mapping)) gfp |= __GFP_WRITE; -if (fgp_flags &FGP_NOFS ) - gfp &= ~__GFP_FS;if (fgp_flags &FGP_NOWAIT ) { gfp &= ~GFP_KERNEL ; gfp |=GFP_NOWAIT ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 9/9] filemap: Remove FGP_NOFS
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Undeclared identifier 'FGP_NOFS' causes compilation failure in f2fs, and trivial removal introduces…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Undeclared identifier 'FGP_NOFS' causes compilation failure in f2fs, and trivial removal introduces a filesystem reclaim deadlock. --
commit 1683abf08ae4fef5fa05b44d804e2e0ac6771ec5
Author: Matthew Wilcox (Oracle) <[email protected]>
filemap: Remove FGP_NOFS
This commit removes the FGP_NOFS flag and its associated __GFP_FS clearing logic, stating that there are no more users of it.
Quoted 4 lines
include/linux/pagemap.h +0
-0
[ ... ]
include/linux/pagemap.h typedef unsigned int __bitwise fgf_t; · line 713
Quoted 5 lines
include/linux/pagemap.h +0
-1@@ -710,11 +709,10 @@ typedef unsigned int __bitwise fgf_t;#define FGP_LOCK ((__force fgf_t)0x00000002)#define FGP_CREAT ((__force fgf_t)0x00000004)#define FGP_WRITE ((__force fgf_t)0x00000008) -#define FGP_NOFS ((__force fgf_t)0x00000010)
[Severity: Low]
Does this removal break the build for f2fs?
Looking at f2fs_write_begin() in fs/f2fs/data.c, it still relies on the removed FGP_NOFS flag:
folio = f2fs_filemap_get_folio(mapping, index,
FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_NOFS, mapping_gfp_mask(mapping));
mm/filemap.c struct folio *__filemap_get_folio_mpol(struct address_space *mapping, · line 1998
Quoted 10 lines
mm/filemap.c +0
-2@@ -1994,8 +1994,6 @@ struct folio *__filemap_get_folio_mpol(struct address_space *mapping,if ((fgp_flags &FGP_WRITE ) &&mapping_can_writeback (mapping)) gfp |= __GFP_WRITE; -if (fgp_flags &FGP_NOFS ) - gfp &= ~__GFP_FS;
[Severity: Low]
If the build failure is trivially fixed by simply deleting FGP_NOFS from f2fs_write_begin() without adding a memalloc_nofs_save() wrapper, will this introduce an ABBA deadlock during direct reclaim?
Because __filemap_get_folio_mpol() no longer clears __GFP_FS, this could allow __GFP_FS to bleed into the allocator while f2fs calls this holding internal filesystem locks.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Matthew Wilcox
maintainerRe: [PATCH 9/9] filemap: Remove FGP_NOFS
Quoted 9 lines · On Sun, Aug 30, 2026 at 04:27:42AM +0000, [email protected] wrote:
[Severity: Low]
Does this removal break the build for f2fs?Looking at
f2fs_write_begin()infs/f2fs/data.c, it still relies on the removedFGP_NOFSflag:folio = f2fs_filemap_get_folio(mapping, index,
FGP_LOCK|FGP_WRITE|FGP_CREAT|FGP_NOFS,mapping_gfp_mask(mapping));
Hi Sashiko, the tree you've applied this to for analysis doesn't include commit 35e5554ec14b which removes this flag. That commit was merged into Linus' tree in 115bd364ab20, please update your tree.
Re: [PATCH 9/9] filemap: Remove FGP_NOFS
In reply to Matthew Wilcox (Oracle)Quoted 3 lines · Matthew Wilcox (Oracle)
There are no more users of
FGP_NOFS. Remove it.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 49 lines
--- include/linux/pagemap.h | 10 ++++------ mm/filemap.c | 2 -- 2 files changed, 4 insertions(+), 8 deletions(-)include/linux/pagemap.h +4
-6@@ -697,7 +697,6 @@ pgoff_t page_cache_prev_miss(struct address_space *mapping,* folio is already in cache. If the folio was allocated, unlock it * before returning so the caller can do the same dance. * * %FGP_WRITE - The folio will be written to by the caller. -* * %FGP_NOFS - __GFP_FS will get cleared in gfp. * * %FGP_NOWAIT - Don't block on the folio lock. * * %FGP_STABLE - Wait for the folio to be stable (finished writeback) * * %FGP_DONTCACHE - Uncached buffered IO @@ -710,11 +709,10 @@ typedef unsigned int __bitwise fgf_t;#define FGP_LOCK ((__force fgf_t)0x00000002)#define FGP_CREAT ((__force fgf_t)0x00000004)#define FGP_WRITE ((__force fgf_t)0x00000008) -#define FGP_NOFS ((__force fgf_t)0x00000010) -#define FGP_NOWAIT ((__force fgf_t)0x00000020) -#define FGP_FOR_MMAP ((__force fgf_t)0x00000040) -#define FGP_STABLE ((__force fgf_t)0x00000080) -#define FGP_DONTCACHE ((__force fgf_t)0x00000100) +#define FGP_NOWAIT ((__force fgf_t)0x00000010) +#define FGP_FOR_MMAP ((__force fgf_t)0x00000020) +#define FGP_STABLE ((__force fgf_t)0x00000040) +#define FGP_DONTCACHE ((__force fgf_t)0x00000080)#define FGF_GET_ORDER (fgf) (((__force unsigned)fgf) >> 26)/* top 6 bits */ #define FGP_WRITEBEGIN (FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE)mm/filemap.c +0
-2@@ -1994,8 +1994,6 @@ struct folio *__filemap_get_folio_mpol(struct address_space *mapping,if ((fgp_flags &FGP_WRITE ) &&mapping_can_writeback (mapping)) gfp |= __GFP_WRITE; -if (fgp_flags &FGP_NOFS ) - gfp &= ~__GFP_FS;if (fgp_flags &FGP_NOWAIT ) { gfp &= ~GFP_KERNEL ; gfp |=GFP_NOWAIT ;Signature · 1 line
2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR