Re: [PATCH 2/2] f2fs: quota: use memalloc_nofs_{save,restore} instead of FGP_NOFS

From: Matthew Wilcox

Date: Mon Jul 06 2026 - 12:38:09 EST


On Mon, Jul 06, 2026 at 05:59:43PM +0800, Chao Yu wrote:
> FGP_NOFS could be removed later, let's use memalloc_nofs_{save,restore}
> instead, which is recommended to be used to avoid potential deadlock
> when memory allocation in f2fs_quota_write() will call into filesystem
> interface again, e.g. .writepages, evict_inode, shrinker due to
> complicated lock race condition.

I think we need to be clear on why we need the memalloc_nofs_save()
call here. What problem would it cause if we did call into the
filesystem to reclaim memory?

I suspect this is the wrong place to insert this call and it should be
near the lock that causes the problem. I've attempted a rewrite of the
memalloc_nofs_save documentation; let me know what you think:

/**
* memalloc_nofs_save - Prevent recursion into the filesystem.
*
* 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.
*/