The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Chao Yu <chao@kernel.org>
To: Matthew Wilcox <willy@infradead.org>, jaegeuk@kernel.org
Cc: chao@kernel.org, linux-f2fs-devel@lists.sourceforge.net,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] f2fs: quota: use memalloc_nofs_{save,restore} instead of FGP_NOFS
Date: Tue, 7 Jul 2026 17:44:15 +0800	[thread overview]
Message-ID: <bdea067e-6159-405b-8eab-ef7eacd623e0@kernel.org> (raw)
In-Reply-To: <aku5T3TYNMDlJMns@casper.infradead.org>

On 7/6/26 22:18, Matthew Wilcox wrote:
> 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?

Hmm, after taking another look at the code, I suspect below deadlock may
occur w/o NOFS flag?

User Sync (Thread 1)
- f2fs_do_quota_sync
 - f2fs_lock_op
  : down_read(cp_rwsem) [HELD] (1st Read Lock)
 - f2fs_quota_sync_file
  - dquot_writeback_dquots
   - ...
    - f2fs_quota_write
     - f2fs_write_begin
      - f2fs_filemap_get_folio
       - __filemap_get_folio
        - ... (GFP_FS allocation triggers reclaim)
         - try_to_free_pages()
          - shrink_node()
           - shrink_lruvec()
            - shrink_page_list()
             - pageout()  <-- Reclaim decides to write back a dirty F2FS data page
              - f2fs_writepages()
               - f2fs_write_cache_pages()
                - f2fs_write_single_data_page(allow_balance = true)
                 - f2fs_balance_fs(need = true)
                  - f2fs_gc()
                   - f2fs_write_checkpoint()
                    - block_operations()
                     - f2fs_lock_all()
                      : down_write(cp_rwsem) [BLOCKED] (Self-deadlock)

To Jaegeuk, please help to double check this. :)

> 
> 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.
>  */

It makes sense to me, thanks for updating.

Thanks,

  reply	other threads:[~2026-07-07  9:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-06  9:59 [PATCH 1/2] f2fs: quota: do not use GFP_NOFS in f2fs_quota_read() Chao Yu
2026-07-06  9:59 ` [PATCH 2/2] f2fs: quota: use memalloc_nofs_{save,restore} instead of FGP_NOFS Chao Yu
2026-07-06 14:18   ` Matthew Wilcox
2026-07-07  9:44     ` Chao Yu [this message]
2026-07-07  9:53       ` Chao Yu
2026-07-06 12:55 ` [PATCH 1/2] f2fs: quota: do not use GFP_NOFS in f2fs_quota_read() Matthew Wilcox
2026-07-07  1:58   ` Chao Yu

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=bdea067e-6159-405b-8eab-ef7eacd623e0@kernel.org \
    --to=chao@kernel.org \
    --cc=jaegeuk@kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=willy@infradead.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