From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
To: Christian Brauner <christian@brauner.io>
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>,
Jan Kara <jack@suse.cz>, Gao Xiang <xiang@kernel.org>,
Chao Yu <chao@kernel.org>, "Theodore Tso" <tytso@mit.edu>,
Trond Myklebust <trondmy@kernel.org>,
Anna Schumaker <anna@kernel.org>,
Namjae Jeon <linkinjeon@kernel.org>,
Hyunchul Lee <hyc.lee@gmail.com>,
Phillip Lougher <phillip@squashfs.org.uk>,
linux-erofs@lists.ozlabs.org, linux-kernel@vger.kernel.org,
linux-ext4@vger.kernel.org, linux-nfs@vger.kernel.org,
ntfs@lists.linux.dev, linux-fsdevel@vger.kernel.org,
linux-mm@kvack.org
Subject: [PATCH 1/9] mm: Improve memalloc_nofs_save() documentation
Date: Sun, 30 Aug 2026 05:18:48 +0100 [thread overview]
Message-ID: <20260830041901.2668-2-willy@infradead.org> (raw)
In-Reply-To: <20260830041901.2668-1-willy@infradead.org>
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.
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
include/linux/sched/mm.h | 31 +++++++++++++++++++++++--------
mm/readahead.c | 14 ++------------
2 files changed, 25 insertions(+), 20 deletions(-)
diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h
index d7c6a942aa7e..f76be1141b06 100644
--- a/include/linux/sched/mm.h
+++ b/include/linux/sched/mm.h
@@ -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.
*/
diff --git a/mm/readahead.c b/mm/readahead.c
index 6e5563290287..9c116d4ba963 100644
--- a/mm/readahead.c
+++ b/mm/readahead.c
@@ -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);
/*
--
2.47.3
next prev parent reply other threads:[~2026-08-30 4:19 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-30 4:18 [PATCH 0/9] Remove FGP_NOFS Matthew Wilcox (Oracle)
2026-08-30 4:18 ` Matthew Wilcox (Oracle) [this message]
2026-08-30 4:21 ` [PATCH 1/9] mm: Improve memalloc_nofs_save() documentation sashiko-bot
2026-08-30 4:18 ` [PATCH 2/9] ext4: Remove uses of FGP_NOFS Matthew Wilcox (Oracle)
2026-08-30 4:27 ` sashiko-bot
2026-08-30 4:18 ` [PATCH 3/9] erofs: Remove redundant FGP_NOFS Matthew Wilcox (Oracle)
2026-08-30 4:24 ` sashiko-bot
2026-08-30 4:18 ` [PATCH 4/9] nfs: " Matthew Wilcox (Oracle)
2026-08-30 4:26 ` sashiko-bot
2026-08-30 4:18 ` [PATCH 5/9] ntfs: Remove FGP_NOFS from __ntfs_inode_resident_attr_pwrite() Matthew Wilcox (Oracle)
2026-08-30 4:39 ` sashiko-bot
2026-08-30 4:18 ` [PATCH 6/9] ntfs: Remove redundant FGP_NOFS from ntfs_wof_collect_dest() Matthew Wilcox (Oracle)
2026-08-30 4:25 ` sashiko-bot
2026-08-30 4:18 ` [PATCH 7/9] squashfs: Remove use of FGP_NOFS Matthew Wilcox (Oracle)
2026-08-30 4:33 ` sashiko-bot
2026-08-30 4:18 ` [PATCH 8/9] filemap: Remove redundant FGP_NOFS Matthew Wilcox (Oracle)
2026-08-30 4:25 ` sashiko-bot
2026-08-30 4:18 ` [PATCH 9/9] filemap: Remove FGP_NOFS Matthew Wilcox (Oracle)
2026-08-30 4:27 ` sashiko-bot
2026-08-30 4:32 ` Matthew Wilcox
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260830041901.2668-2-willy@infradead.org \
--to=willy@infradead.org \
--cc=anna@kernel.org \
--cc=chao@kernel.org \
--cc=christian@brauner.io \
--cc=hyc.lee@gmail.com \
--cc=jack@suse.cz \
--cc=linkinjeon@kernel.org \
--cc=linux-erofs@lists.ozlabs.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-nfs@vger.kernel.org \
--cc=ntfs@lists.linux.dev \
--cc=phillip@squashfs.org.uk \
--cc=trondmy@kernel.org \
--cc=tytso@mit.edu \
--cc=xiang@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox