From nobody Sun Jun 21 04:20:57 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1879D33CEA7; Tue, 7 Apr 2026 14:55:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775573746; cv=none; b=OvFRM6hmX03otud3tOR6xIaJDTEVQBrlJPliozR8JYnETz1KQpcr9uR8r2T5lrHfBHeS/ao/L0OBFydcO9wDaD9ANjIMBSuuxhIzWrjwFB7un0qgbgA3rfjrYn+HN95TIGsLj04eakeAd5JCrJsYoCLEzcuos9oSve4RPk6iLm4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775573746; c=relaxed/simple; bh=Fv9PtzN7xCSTNsq4ANj8AT1swf3W60piN2PSJ2CQe5E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BTZhJ7DA9yUJlrUmhhn06g2nlZIS7iS2qPK8Dfezwc0HXFKLrEWAZAlwHrrSelVdB0locGTKN5u1icQkF0m2g29QJysC6t7/1SxoNQzrz8KV7Q/9TUpVYy5xZXqSoK5sTOgkikbGniGromq+sn1d4nohKValPe98LDEo2xOg/sY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qKViqKdN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qKViqKdN" Received: by smtp.kernel.org (Postfix) with ESMTPS id BB0BDC2BCAF; Tue, 7 Apr 2026 14:55:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775573745; bh=Fv9PtzN7xCSTNsq4ANj8AT1swf3W60piN2PSJ2CQe5E=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=qKViqKdNbUTPQihtHtm1YVs6+OE/BmF1bRVTdVUeBbqshZPcUsmBWB/POqBPmhTi4 Hz/pt9Xs46mhPrQ3ILw+Nm7UHZPtF6fjBoE1mNur97AaHB9X+yeoU8E7HhLuWM1EZM ObA2ayE7aX6LpG3e5R8ewXHVNc3s3ADDCtqY7L5wz3+dq3vC4QGPQkJ+gh16zEC+QE nVlGDc0sw9b2HPKiU8Rjq+WJ4te1fzZc9OsgegPqBAi8E7Usrr/wWs1rwIMOLRp5r4 XAeLqyow15dqL/rmw1LJoDYf37JzoOzwuT00PPRhlzoBTlsM5SkxQVh5g1+XJbeA/7 kUDZyuZ6CmiRQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id ABC60FEEF49; Tue, 7 Apr 2026 14:55:45 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 07 Apr 2026 22:55:42 +0800 Subject: [PATCH RFC 1/2] mm, swap: fix potential race of charging into the wrong memcg Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260407-swap-memcg-fix-v1-1-a473ce2e5bb8@tencent.com> References: <20260407-swap-memcg-fix-v1-0-a473ce2e5bb8@tencent.com> In-Reply-To: <20260407-swap-memcg-fix-v1-0-a473ce2e5bb8@tencent.com> To: linux-mm@kvack.org Cc: Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Chris Li , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Johannes Weiner , Alexandre Ghiti , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Hugh Dickins , Baolin Wang , Chuanhua Han , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Kairui Song X-Mailer: b4 0.15.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775573744; l=6871; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=8waYhVG/zmYZKbTD5LXs0veHtD/HsXryyRZUBo5GKyE=; b=irCjKZJcfH1APdFUGYgGW2UOdJ94anKzvDXlkVQVoeRoDu0PD7sDKOyFiX0KbC3RdeQzjyai6 YgLFnp1vTe9BhgDMcWBN37tGPiBPT9o90xyn/zf486gb5TKWojQBGk5 X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Swapin folios are allocated and charged without adding to the swap cache. So it is possible that the corresponding swap slot will be freed, then get allocated again by another memory cgroup. By that time, continuing to use the previously charged folio will be risky. Usually, this won't cause an issue since the upper-level users of the swap entry, the page table, or mapping, will change if the swap entry is freed. But, it's possible the page table just happened to be reusing the same swap entry too, and if that was used by another cgroup, then that's a problem. The chance is extremely low, previously this issue is limited to SYNCHRONOUS_IO devices. But recent commit 9acbe135588e ("mm/swap: fix swap cache memcg accounting") extended the same pattern, charging the folio without adding the folio to swap cache first. The chance is still extremely low, but in theory, it is more common. So to fix that, keep the pattern introduced by commit 2732acda82c9 ("mm, swap: use swap cache as the swap in synchronize layer"), always uses swap cache as the synchronize layer first, and do the charge afterward. And fix the issue that commit 9acbe135588e ("mm/swap: fix swap cache memcg accounting") is trying to fix by separating the statistic part out. This commit only fixes the issue for non SYNCHRONOUS_IO devices. Another separate fix is needed for these devices. Fixes: 9acbe135588e ("mm/swap: fix swap cache memcg accounting") Fixes: 2732acda82c9 ("mm, swap: use swap cache as the swap in synchronize l= ayer") Signed-off-by: Kairui Song --- mm/swap_state.c | 53 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index 1415a5c54a43..c53d16b87a98 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -137,8 +137,8 @@ void *swap_cache_get_shadow(swp_entry_t entry) return NULL; } =20 -void __swap_cache_add_folio(struct swap_cluster_info *ci, - struct folio *folio, swp_entry_t entry) +static void __swap_cache_do_add_folio(struct swap_cluster_info *ci, + struct folio *folio, swp_entry_t entry) { unsigned int ci_off =3D swp_cluster_offset(entry), ci_end; unsigned long nr_pages =3D folio_nr_pages(folio); @@ -159,7 +159,14 @@ void __swap_cache_add_folio(struct swap_cluster_info *= ci, folio_ref_add(folio, nr_pages); folio_set_swapcache(folio); folio->swap =3D entry; +} + +void __swap_cache_add_folio(struct swap_cluster_info *ci, + struct folio *folio, swp_entry_t entry) +{ + unsigned long nr_pages =3D folio_nr_pages(folio); =20 + __swap_cache_do_add_folio(ci, folio, entry); node_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages); lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr_pages); } @@ -207,7 +214,7 @@ static int swap_cache_add_folio(struct folio *folio, sw= p_entry_t entry, if (swp_tb_is_shadow(old_tb)) shadow =3D swp_tb_to_shadow(old_tb); } while (++ci_off < ci_end); - __swap_cache_add_folio(ci, folio, entry); + __swap_cache_do_add_folio(ci, folio, entry); swap_cluster_unlock(ci); if (shadowp) *shadowp =3D shadow; @@ -219,7 +226,7 @@ static int swap_cache_add_folio(struct folio *folio, sw= p_entry_t entry, } =20 /** - * __swap_cache_del_folio - Removes a folio from the swap cache. + * __swap_cache_do_del_folio - Removes a folio from the swap cache. * @ci: The locked swap cluster. * @folio: The folio. * @entry: The first swap entry that the folio corresponds to. @@ -231,8 +238,9 @@ static int swap_cache_add_folio(struct folio *folio, sw= p_entry_t entry, * Context: Caller must ensure the folio is locked and in the swap cache * using the index of @entry, and lock the cluster that holds the entries. */ -void __swap_cache_del_folio(struct swap_cluster_info *ci, struct folio *fo= lio, - swp_entry_t entry, void *shadow) +static void __swap_cache_do_del_folio(struct swap_cluster_info *ci, + struct folio *folio, + swp_entry_t entry, void *shadow) { int count; unsigned long old_tb; @@ -265,8 +273,6 @@ void __swap_cache_del_folio(struct swap_cluster_info *c= i, struct folio *folio, =20 folio->swap.val =3D 0; folio_clear_swapcache(folio); - node_stat_mod_folio(folio, NR_FILE_PAGES, -nr_pages); - lruvec_stat_mod_folio(folio, NR_SWAPCACHE, -nr_pages); =20 if (!folio_swapped) { __swap_cluster_free_entries(si, ci, ci_start, nr_pages); @@ -279,6 +285,16 @@ void __swap_cache_del_folio(struct swap_cluster_info *= ci, struct folio *folio, } } =20 +void __swap_cache_del_folio(struct swap_cluster_info *ci, struct folio *fo= lio, + swp_entry_t entry, void *shadow) +{ + unsigned long nr_pages =3D folio_nr_pages(folio); + + __swap_cache_do_del_folio(ci, folio, entry, shadow); + node_stat_mod_folio(folio, NR_FILE_PAGES, -nr_pages); + lruvec_stat_mod_folio(folio, NR_SWAPCACHE, -nr_pages); +} + /** * swap_cache_del_folio - Removes a folio from the swap cache. * @folio: The folio. @@ -452,7 +468,7 @@ void swap_update_readahead(struct folio *folio, struct = vm_area_struct *vma, * __swap_cache_prepare_and_add - Prepare the folio and add it to swap cac= he. * @entry: swap entry to be bound to the folio. * @folio: folio to be added. - * @gfp: memory allocation flags for charge, can be 0 if @charged if true. + * @gfp: memory allocation flags for charge, can be 0 if @charged is true. * @charged: if the folio is already charged. * * Update the swap_map and add folio as swap cache, typically before swapi= n. @@ -466,16 +482,15 @@ static struct folio *__swap_cache_prepare_and_add(swp= _entry_t entry, struct folio *folio, gfp_t gfp, bool charged) { + unsigned long nr_pages =3D folio_nr_pages(folio); struct folio *swapcache =3D NULL; + struct swap_cluster_info *ci; void *shadow; int ret; =20 __folio_set_locked(folio); __folio_set_swapbacked(folio); =20 - if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) - goto failed; - for (;;) { ret =3D swap_cache_add_folio(folio, entry, &shadow); if (!ret) @@ -496,6 +511,20 @@ static struct folio *__swap_cache_prepare_and_add(swp_= entry_t entry, goto failed; } =20 + if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) { + /* We might lose the shadow here, but that's fine */ + ci =3D swap_cluster_get_and_lock(folio); + __swap_cache_do_del_folio(ci, folio, entry, NULL); + swap_cluster_unlock(ci); + + /* __swap_cache_do_del_folio doesn't put the refs */ + folio_ref_sub(folio, nr_pages); + goto failed; + } + + node_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages); + lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr_pages); + memcg1_swapin(entry, folio_nr_pages(folio)); if (shadow) workingset_refault(folio, shadow); --=20 2.53.0 From nobody Sun Jun 21 04:20:57 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 188C333CEA8; Tue, 7 Apr 2026 14:55:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775573746; cv=none; b=BuONy2da5V/qvKLYXrraZM9BVpMc2eIZb1S8nUNb8YmC1x3z4MX9IU0prYJKf5Tt1QCK4zC1nrSVKzLYpoThO2ve2nuCgg7u2qoZAv9hFSd1UXXATCAWeHm6TkmGhMS0BBfBRc4IDAHtVbhQQwJ+JqFlL32dqi7dTC+qkLBxD0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775573746; c=relaxed/simple; bh=gZSdq43ylN8hfqryWBeuU1CuwBkem5XZlHWCIIzfBHs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ze+T4eazUZ99a0zDoa+2h2kvsqPtj2j6lmi0NJY06CufJUeQh+b72Siv0DyV/3sJXPt8iq3ucjrgpWqMQaE1QoLWV0ekSlaHWGEd0JvmY5BJv1mwmhIWhykNLoaV+RmyZ/PzY2O5+JvNzVVGjrdd+Lp10OcP/J+ZRQ7xv4mcKEc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=p3PJoe5i; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="p3PJoe5i" Received: by smtp.kernel.org (Postfix) with ESMTPS id CC89AC2BCB1; Tue, 7 Apr 2026 14:55:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775573745; bh=gZSdq43ylN8hfqryWBeuU1CuwBkem5XZlHWCIIzfBHs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=p3PJoe5iyZEDTbjG6OMNcQFWzPcNBl/hkk4K7+UpjvSV1TAO4Y04DLDgp8fM2bt7d oJKbV8Ic8TU9BxmsV0Q54RGD1+O6B5z0ka3ZUCkKCCyiP85fEGC696zYF3QNzwKN0J HDRAcTuMMjPYUcTD0g4wZ/ZvXbfuAUsxODFw0pg1f60yxOf/B7+EnA+NTqK0lP2QgC gAqiQaYUQuhM/z4Zotix5i31oG7xo9LiG7VsaZRFvGJttbrjr6fL/gtFPYegR+9RZr j0T2j0NPdDRD4rP9XPxCzANHLURuqjOzZ4TVtbyvhFzLkb2ZxK9CQFeZYQPz9btP70 J0xDAJRLXeCrA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1DA2FEEF56; Tue, 7 Apr 2026 14:55:45 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 07 Apr 2026 22:55:43 +0800 Subject: [PATCH RFC 2/2] mm, swap: fix race of charging into the wrong memcg for THP Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260407-swap-memcg-fix-v1-2-a473ce2e5bb8@tencent.com> References: <20260407-swap-memcg-fix-v1-0-a473ce2e5bb8@tencent.com> In-Reply-To: <20260407-swap-memcg-fix-v1-0-a473ce2e5bb8@tencent.com> To: linux-mm@kvack.org Cc: Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Chris Li , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Johannes Weiner , Alexandre Ghiti , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Hugh Dickins , Baolin Wang , Chuanhua Han , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Kairui Song X-Mailer: b4 0.15.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775573744; l=9867; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=X/ek0pSZoNxTKZ9tG09c+ZJJhzY+pVd49UQb7w0YLRg=; b=TEFcKwourn71DBqm8jy/z51c4t+8wplThkHDyKqJx0EX8IOsS+2FUkCNMMf6qaYfwO3XXR8pv JTm46EI/SigDaZbSECo8iymi1myXiHV3UPIKJ8ozTjGjcdqTbh+ysdB X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song During THP swapin via the SYNCHRONOUS_IO path, the folio is allocated and charged to a memcg before being inserted into the swap cache. Between allocation and swap cache insertion, the page table can change under us (we don't hold the PTE lock), so the swap entry may be freed and reused by a different cgroup. This causes the folio to be charged to the wrong memcg. Shmem also has a similar issue. Usually, the double check of the page table will catch this, but the same page table entry may reuse the swap entry, but by a different cgroup. The chance is extremely low, requiring a set of rare time windows to hit in a row, but it is totally possible. Fix this by charging the folio after it is inserted and stabilized in the swap cache. This would also improve the performance and simplify the code. Also remove the now-stale comment about memcg charging of swapin Now we always charge the folio after adding it to the swap cache. Previously it has to do this before adding to swap cache for maintaining the per-memcg swapcache stat. We have decoupled the stat update from adding the folio during swapin in previous commit so this is fine now. Fixes: 242d12c981745 ("mm: support large folios swap-in for sync io devices= ") Signed-off-by: Kairui Song --- mm/memcontrol.c | 3 +-- mm/memory.c | 53 +++++++++++++++++++++++------------------------------ mm/shmem.c | 15 ++++----------- mm/swap.h | 5 +++-- mm/swap_state.c | 17 +++++++++-------- 5 files changed, 40 insertions(+), 53 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index c3d98ab41f1f..21caed15c9f5 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5067,8 +5067,7 @@ int mem_cgroup_charge_hugetlb(struct folio *folio, gf= p_t gfp) * @gfp: reclaim mode * @entry: swap entry for which the folio is allocated * - * This function charges a folio allocated for swapin. Please call this be= fore - * adding the folio to the swapcache. + * This function charges a folio allocated for swapin. * * Returns 0 on success. Otherwise, an error code is returned. */ diff --git a/mm/memory.c b/mm/memory.c index ea6568571131..6d5b0c10ac8e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4595,22 +4595,8 @@ static vm_fault_t handle_pte_marker(struct vm_fault = *vmf) =20 static struct folio *__alloc_swap_folio(struct vm_fault *vmf) { - struct vm_area_struct *vma =3D vmf->vma; - struct folio *folio; - softleaf_t entry; - - folio =3D vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vmf->address); - if (!folio) - return NULL; - - entry =3D softleaf_from_pte(vmf->orig_pte); - if (mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, - GFP_KERNEL, entry)) { - folio_put(folio); - return NULL; - } - - return folio; + return vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, + vmf->vma, vmf->address); } =20 #ifdef CONFIG_TRANSPARENT_HUGEPAGE @@ -4736,13 +4722,8 @@ static struct folio *alloc_swap_folio(struct vm_faul= t *vmf) while (orders) { addr =3D ALIGN_DOWN(vmf->address, PAGE_SIZE << order); folio =3D vma_alloc_folio(gfp, order, vma, addr); - if (folio) { - if (!mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, - gfp, entry)) - return folio; - count_mthp_stat(order, MTHP_STAT_SWPIN_FALLBACK_CHARGE); - folio_put(folio); - } + if (folio) + return folio; count_mthp_stat(order, MTHP_STAT_SWPIN_FALLBACK); order =3D next_order(&orders, order); } @@ -4858,18 +4839,30 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio =3D swap_cache_get_folio(entry); if (folio) swap_update_readahead(folio, vma, vmf->address); + if (!folio) { if (data_race(si->flags & SWP_SYNCHRONOUS_IO)) { + gfp_t gfp =3D GFP_HIGHUSER_MOVABLE; + folio =3D alloc_swap_folio(vmf); if (folio) { - /* - * folio is charged, so swapin can only fail due - * to raced swapin and return NULL. - */ - swapcache =3D swapin_folio(entry, folio); - if (swapcache !=3D folio) + if (folio_test_large(folio)) + gfp =3D vma_thp_gfp_mask(vma); + swapcache =3D swapin_folio(entry, folio, gfp); + if (swapcache) { + /* We might hit with another cached swapin */ + if (swapcache !=3D folio) + folio_put(folio); + folio =3D swapcache; + } else if (folio_test_large(folio)) { + /* THP swapin failed, try order 0 */ + folio_put(folio); + folio =3D swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vmf); + } else { + /* order 0 swapin failure, abort */ folio_put(folio); - folio =3D swapcache; + folio =3D NULL; + } } } else { folio =3D swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vmf); diff --git a/mm/shmem.c b/mm/shmem.c index 5aa43657886c..bc67b04b9de4 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2071,22 +2071,15 @@ static struct folio *shmem_swap_alloc_folio(struct = inode *inode, goto fallback; } =20 - if (mem_cgroup_swapin_charge_folio(new, vma ? vma->vm_mm : NULL, - alloc_gfp, entry)) { - folio_put(new); - new =3D ERR_PTR(-ENOMEM); - goto fallback; - } - - swapcache =3D swapin_folio(entry, new); + swapcache =3D swapin_folio(entry, new, alloc_gfp); if (swapcache !=3D new) { folio_put(new); if (!swapcache) { /* - * The new folio is charged already, swapin can - * only fail due to another raced swapin. + * Fail with -ENOMEM by default, caller will + * correct it to -EEXIST if mapping changed. */ - new =3D ERR_PTR(-EEXIST); + new =3D ERR_PTR(-ENOMEM); goto fallback; } } diff --git a/mm/swap.h b/mm/swap.h index a77016f2423b..90f1edabb73a 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -300,7 +300,7 @@ struct folio *swap_cluster_readahead(swp_entry_t entry,= gfp_t flag, struct mempolicy *mpol, pgoff_t ilx); struct folio *swapin_readahead(swp_entry_t entry, gfp_t flag, struct vm_fault *vmf); -struct folio *swapin_folio(swp_entry_t entry, struct folio *folio); +struct folio *swapin_folio(swp_entry_t entry, struct folio *folio, gfp_t f= lag); void swap_update_readahead(struct folio *folio, struct vm_area_struct *vma, unsigned long addr); =20 @@ -433,7 +433,8 @@ static inline struct folio *swapin_readahead(swp_entry_= t swp, gfp_t gfp_mask, return NULL; } =20 -static inline struct folio *swapin_folio(swp_entry_t entry, struct folio *= folio) +static inline struct folio *swapin_folio(swp_entry_t entry, + struct folio *folio, gfp_t flag) { return NULL; } diff --git a/mm/swap_state.c b/mm/swap_state.c index c53d16b87a98..d24a7a3482ec 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -468,8 +468,7 @@ void swap_update_readahead(struct folio *folio, struct = vm_area_struct *vma, * __swap_cache_prepare_and_add - Prepare the folio and add it to swap cac= he. * @entry: swap entry to be bound to the folio. * @folio: folio to be added. - * @gfp: memory allocation flags for charge, can be 0 if @charged is true. - * @charged: if the folio is already charged. + * @gfp: memory allocation flags for charge. * * Update the swap_map and add folio as swap cache, typically before swapi= n. * All swap slots covered by the folio must have a non-zero swap count. @@ -480,7 +479,7 @@ void swap_update_readahead(struct folio *folio, struct = vm_area_struct *vma, */ static struct folio *__swap_cache_prepare_and_add(swp_entry_t entry, struct folio *folio, - gfp_t gfp, bool charged) + gfp_t gfp) { unsigned long nr_pages =3D folio_nr_pages(folio); struct folio *swapcache =3D NULL; @@ -511,12 +510,14 @@ static struct folio *__swap_cache_prepare_and_add(swp= _entry_t entry, goto failed; } =20 - if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) { + if (mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) { /* We might lose the shadow here, but that's fine */ ci =3D swap_cluster_get_and_lock(folio); __swap_cache_do_del_folio(ci, folio, entry, NULL); swap_cluster_unlock(ci); =20 + count_mthp_stat(folio_order(folio), MTHP_STAT_SWPIN_FALLBACK_CHARGE); + /* __swap_cache_do_del_folio doesn't put the refs */ folio_ref_sub(folio, nr_pages); goto failed; @@ -578,7 +579,7 @@ struct folio *swap_cache_alloc_folio(swp_entry_t entry,= gfp_t gfp_mask, if (!folio) return NULL; /* Try add the new folio, returns existing folio or NULL on failure. */ - result =3D __swap_cache_prepare_and_add(entry, folio, gfp_mask, false); + result =3D __swap_cache_prepare_and_add(entry, folio, gfp_mask); if (result =3D=3D folio) *new_page_allocated =3D true; else @@ -589,7 +590,7 @@ struct folio *swap_cache_alloc_folio(swp_entry_t entry,= gfp_t gfp_mask, /** * swapin_folio - swap-in one or multiple entries skipping readahead. * @entry: starting swap entry to swap in - * @folio: a new allocated and charged folio + * @folio: a new allocated folio * * Reads @entry into @folio, @folio will be added to the swap cache. * If @folio is a large folio, the @entry will be rounded down to align @@ -600,14 +601,14 @@ struct folio *swap_cache_alloc_folio(swp_entry_t entr= y, gfp_t gfp_mask, * to order 0. Else, if another folio was already added to the swap cache, * return that swap cache folio instead. */ -struct folio *swapin_folio(swp_entry_t entry, struct folio *folio) +struct folio *swapin_folio(swp_entry_t entry, struct folio *folio, gfp_t g= fp) { struct folio *swapcache; pgoff_t offset =3D swp_offset(entry); unsigned long nr_pages =3D folio_nr_pages(folio); =20 entry =3D swp_entry(swp_type(entry), round_down(offset, nr_pages)); - swapcache =3D __swap_cache_prepare_and_add(entry, folio, 0, true); + swapcache =3D __swap_cache_prepare_and_add(entry, folio, gfp); if (swapcache =3D=3D folio) swap_read_folio(folio, NULL); return swapcache; --=20 2.53.0