From: Baoquan He <bhe@redhat.com>
To: Kairui Song <kasong@tencent.com>
Cc: linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
Chris Li <chrisl@kernel.org>, Barry Song <v-songbaohua@oppo.com>,
Hugh Dickins <hughd@google.com>,
Yosry Ahmed <yosryahmed@google.com>,
"Huang, Ying" <ying.huang@linux.alibaba.com>,
Nhat Pham <nphamcs@gmail.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Kalesh Singh <kaleshsingh@google.com>,
Matthew Wilcox <willy@infradead.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 7/7] mm, swap: simplify folio swap allocation
Date: Tue, 25 Feb 2025 14:43:21 +0800 [thread overview]
Message-ID: <Z71miSXHnSuW1wHE@MiWiFi-R3L-srv> (raw)
In-Reply-To: <20250224180212.22802-8-ryncsn@gmail.com>
On 02/25/25 at 02:02am, Kairui Song wrote:
......snip...
> @@ -1265,20 +1249,68 @@ swp_entry_t folio_alloc_swap(struct folio *folio)
> goto start_over;
> }
> spin_unlock(&swap_avail_lock);
> -out_failed:
> + return false;
> +}
> +
> +/**
> + * folio_alloc_swap - allocate swap space for a folio
> + * @folio: folio we want to move to swap
> + * @gfp: gfp mask for shadow nodes
> + *
> + * Allocate swap space for the folio and add the folio to the
> + * swap cache.
> + *
> + * Context: Caller needs to hold the folio lock.
> + * Return: Whether the folio was added to the swap cache.
If only returning on whether folio being added or not, it's better to
return bool value for now. Anyway, this is trivial. This whole
patch looks good to me.
Reviewed-by: Baoquan He <bhe@redhat.com>
> + */
> +int folio_alloc_swap(struct folio *folio, gfp_t gfp)
> +{
> + unsigned int order = folio_order(folio);
> + unsigned int size = 1 << order;
> + swp_entry_t entry = {};
> +
> + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
> + VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio);
> +
> + /*
> + * Should not even be attempting large allocations when huge
> + * page swap is disabled. Warn and fail the allocation.
> + */
> + if (order && (!IS_ENABLED(CONFIG_THP_SWAP) || size > SWAPFILE_CLUSTER)) {
> + VM_WARN_ON_ONCE(1);
> + return -EINVAL;
> + }
> +
> + local_lock(&percpu_swap_cluster.lock);
> + if (swap_alloc_fast(&entry, SWAP_HAS_CACHE, order))
> + goto out_alloced;
> + if (swap_alloc_slow(&entry, SWAP_HAS_CACHE, order))
> + goto out_alloced;
> local_unlock(&percpu_swap_cluster.lock);
> - return entry;
> + return -ENOMEM;
>
> out_alloced:
> local_unlock(&percpu_swap_cluster.lock);
> - if (mem_cgroup_try_charge_swap(folio, entry)) {
> - put_swap_folio(folio, entry);
> - entry.val = 0;
> - } else {
> - atomic_long_sub(size, &nr_swap_pages);
> - }
> + if (mem_cgroup_try_charge_swap(folio, entry))
> + goto out_free;
>
> - return entry;
> + /*
> + * XArray node allocations from PF_MEMALLOC contexts could
> + * completely exhaust the page allocator. __GFP_NOMEMALLOC
> + * stops emergency reserves from being allocated.
> + *
> + * TODO: this could cause a theoretical memory reclaim
> + * deadlock in the swap out path.
> + */
> + if (add_to_swap_cache(folio, entry, gfp | __GFP_NOMEMALLOC, NULL))
> + goto out_free;
> +
> + atomic_long_sub(size, &nr_swap_pages);
> + return 0;
> +
> +out_free:
> + put_swap_folio(folio, entry);
> + return -ENOMEM;
> }
>
> static struct swap_info_struct *_swap_info_get(swp_entry_t entry)
....snip....
prev parent reply other threads:[~2025-02-25 6:43 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-24 18:02 [PATCH v2 0/7] mm, swap: remove swap slot cache Kairui Song
2025-02-24 18:02 ` [PATCH v2 1/7] mm, swap: avoid reclaiming irrelevant swap cache Kairui Song
2025-02-24 18:02 ` [PATCH v2 2/7] mm, swap: drop the flag TTRS_DIRECT Kairui Song
2025-02-24 18:02 ` [PATCH v2 3/7] mm, swap: avoid redundant swap device pinning Kairui Song
2025-02-24 18:02 ` [PATCH v2 4/7] mm, swap: don't update the counter up-front Kairui Song
2025-02-25 6:32 ` Baoquan He
2025-02-24 18:02 ` [PATCH v2 5/7] mm, swap: use percpu cluster as allocation fast path Kairui Song
2025-02-25 4:01 ` Baoquan He
2025-02-25 6:38 ` Baoquan He
2025-03-07 2:54 ` Kairui Song
2025-02-24 18:02 ` [PATCH v2 6/7] mm, swap: remove swap slot cache Kairui Song
2025-02-25 6:39 ` Baoquan He
2025-02-24 18:02 ` [PATCH v2 7/7] mm, swap: simplify folio swap allocation Kairui Song
2025-02-25 6:43 ` Baoquan He [this message]
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=Z71miSXHnSuW1wHE@MiWiFi-R3L-srv \
--to=bhe@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=chrisl@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=kaleshsingh@google.com \
--cc=kasong@tencent.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=v-songbaohua@oppo.com \
--cc=willy@infradead.org \
--cc=ying.huang@linux.alibaba.com \
--cc=yosryahmed@google.com \
/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