From nobody Mon Feb 9 17:05:04 2026 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE81133CE82 for ; Fri, 19 Dec 2025 19:44:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766173473; cv=none; b=pVAaYSsIjQGiCjyjuYtlYjJgLbjkyHTNn0cbIXNY0I6ns4dBppjYDGOTQ8jEGp1oZoFJLCsDRhIXB7iEhalxA2SKkePUGF9eLgI2hBhZCZnVlgbwdN488dITCab3T+hberialx2SSB6dpB8/SHvxAxTiCJC3r2pKQC2woQqaU3c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766173473; c=relaxed/simple; bh=gFsjaq/ALU/9N8ndyBcj35EwDezI9teWe0L7EXppRIk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WnaJDbgd+3Jyn7efQnfBoxP6cEXC7m7eleJFfUo3NdvuBpp07/G7U4KgN7M5M6bkU2d0Q07A+z9D1qRNxFcmZiZV/8Wp6xu8vWKnk6LcgpELXA+1M9hr+W7tO/iVCxAN4Z3mHRpq6QlS0gi2qhr/Km3nrJEd6ns/5z8VUco2Fao= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EC8rXeJg; arc=none smtp.client-ip=209.85.214.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EC8rXeJg" Received: by mail-pl1-f196.google.com with SMTP id d9443c01a7336-2a0a95200e8so20392295ad.0 for ; Fri, 19 Dec 2025 11:44:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766173471; x=1766778271; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Wdy65Ou8JbWRDmKXo9PQRW4JzY5cc2LJzS9tndH63Hw=; b=EC8rXeJgJdtzlhQv5XYvdgqEPDJtcjHJYrvXdZxTh1/nzEiQcjAC1HxV4FadrFouYh YyQtHgFWnTxXVBy4SPNPv/u03oUWrXUWVPqADCd1DkpQO3CrNQmyzpOHkQHSYqFcPpFg oEXJEaIeBn4XbC6J95mHem1gv6VKzuwKDFh5lMXGq/uwRiZ8ZzAKfKigDBf9DBMLAUGj +un05lj9n8QvoEWybCDf2bwxGPU1y4KUmth8xiD4I35jgo4q/vNVxDsWjDhbeYFEpjGe LDFbUh/92zUuDmWuJKPUxRDP020l8ixBgxcclMgAO24OUvJhBkbfwTH/hwS7h7BqBVYw /FHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766173471; x=1766778271; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Wdy65Ou8JbWRDmKXo9PQRW4JzY5cc2LJzS9tndH63Hw=; b=Fm2JDDFnfPuq28tj1G2GFFDXcvaGrlypBPMgrdv4LDMJhSMYBlL0bJLRqA6oPAaUP1 0Or4SRs4Ihv7VkSfgtat7eOuA7PdF3WZy127Wsy6zbHqiUXAH+FAGmYoNEUdKAfDyOpb pfSpOGKBR47VXRPOco98qLafLqiEk+eYvJupXj4KVeOnUAIokygeZFAiiBtfmZ7G74zD zxkUvJ/CBupR9DPTQn3LfBvV974RbEKAnH7tTnaN5qQfv7e2lNBtKE9CvFPPylCiy/U7 PiICRtyFm2cdXJU1e9ArG5qasJXQtzEmIGr2/INe/5Ej8MzLH6nBMWyN7XBPefsFMWuY x28A== X-Forwarded-Encrypted: i=1; AJvYcCUYROaFP347yUHdQ/MKA1flhW0qe6Xu+D10ZEbC+MvjUYJqkS/fM8AaVpAXCjgL73tfLJEx3JFnIdeslF4=@vger.kernel.org X-Gm-Message-State: AOJu0YzxL+7y1EbgUyjoC8UOxlylQoClzdonfFJpzWe4IuolPj67IXBV kGbF4KSKdARue5eiJ5ARB/1SfIZ3cW1k3jdIzBKrMoYlqzHOGRROEms3 X-Gm-Gg: AY/fxX77V8fgLXqe39T4vGh1/z5t8QYeqHYppkrRGAiU46fSBsVv+CzO+Cx/KSDXhe3 idRf7y/Co0R3V9SzaOl+gI1J+2xutA+MS3U/zuFkKT190xn7IOxHPn9tX9LZBjS/JJRtjob/QdS G39DXLrM/Yu2YLGAMKj/W4WYEy/AjBzeoRWVBqQ3FdY6VqxIVDOvs0joJKIlJ+dym82kNEqVIwa dAtWONKvqrysWCyGysPyWZugMiZDeeQb+C1Oo2kAzLPcSt/OYLdAvtk48gSM2hv4mQkUgw+KMQu uSu4T3B6AhBtmWsgFzLvZ62jFGwW+6dz3wvkiJvABJeFPAvNbfuE3pv3y2sjX9sfjC8zDcr5/yk BDPvkxACp5Hs5Q7XMLgTbivZ54/wDsILHFXZyMZ/PnZuRvCac681hJyTofKMkoa4kiFC6oKt45L yAW2DrukIbS66jQXwMn4dAjM0fKx+1XTr8PXetjPBoHxj42P4bu7kE X-Google-Smtp-Source: AGHT+IG4CDonbV3GFvhsjxzJk22Gacbkw05BqfVp934Sw3htWv1tWnWtHgdeJa2AVGj6kHEDd3u2wg== X-Received: by 2002:a17:902:ce8b:b0:295:b46f:a6c2 with SMTP id d9443c01a7336-2a2f27325d5mr34529775ad.37.1766173471122; Fri, 19 Dec 2025 11:44:31 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a2f3d76ceesm30170985ad.91.2025.12.19.11.44.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Dec 2025 11:44:30 -0800 (PST) From: Kairui Song Date: Sat, 20 Dec 2025 03:43:32 +0800 Subject: [PATCH v5 03/19] mm, swap: never bypass the swap cache even for SWP_SYNCHRONOUS_IO 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: <20251220-swap-table-p2-v5-3-8862a265a033@tencent.com> References: <20251220-swap-table-p2-v5-0-8862a265a033@tencent.com> In-Reply-To: <20251220-swap-table-p2-v5-0-8862a265a033@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Baoquan He , Barry Song , Chris Li , Nhat Pham , Yosry Ahmed , David Hildenbrand , Johannes Weiner , Youngjun Park , Hugh Dickins , Baolin Wang , Ying Huang , Kemeng Shi , Lorenzo Stoakes , "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, Kairui Song X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1766173451; l=12641; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=+3pLR6DNAmoEtneFzdsLDCbvXyTZwRGJx+xHGBkb1j0=; b=0fD8hCCxok4pcURnS9vGRUviNahfagzA/y3AIqUeiBm+PgPFRAbUucN/XlEBrnO6zGNr7P1pT HASK/jzthWpBtDvvkWWv3RW3Hb1YqcJ8Uw/NPFVuxvTNkyjGBSP1FL7 X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Kairui Song Now the overhead of the swap cache is trivial. Bypassing the swap cache is no longer a valid optimization. So unify the swapin path using the swap cache. This changes the swap in behavior in two observable ways. Readahead is now always disabled for SWP_SYNCHRONOUS_IO devices, which is a huge win for some workloads: We used to rely on `SWP_SYNCHRONOUS_IO && __swap_count(entry) =3D=3D 1` as the indicator to bypass both the swap cache and readahead, the swap count check made bypassing ineffective in many cases, and it's not a good indicator. The limitation existed because the current swap design made it hard to decouple readahead bypassing and swap cache bypassing. We do want to always bypass readahead for SWP_SYNCHRONOUS_IO devices, but bypassing swap cache at the same time will cause repeated IO and memory overhead. Now that swap cache bypassing is gone, this swap count check can be dropped. The second thing here is that this enabled large swapin for all swap entries on SWP_SYNCHRONOUS_IO devices. Previously, the large swap in is also coupled with swap cache bypassing, and so the swap count checking also makes large swapin less effective. Now this is also improved. We will always have large swapin supported for all SWP_SYNCHRONOUS_IO cases. And to catch potential issues with large swapin, especially with page exclusiveness and swap cache, more debug sanity checks and comments are added. But overall, the code is simpler. And new helper and routines will be used by other components in later commits too. And now it's possible to rely on the swap cache layer for resolving synchronization issues, which will also be done by a later commit. Worth mentioning that for a large folio workload, this may cause more serious thrashing. This isn't a problem with this commit, but a generic large folio issue. For a 4K workload, this commit increases the performance. Signed-off-by: Kairui Song --- mm/memory.c | 137 +++++++++++++++++++++-------------------------------= ---- mm/swap.h | 6 +++ mm/swap_state.c | 27 +++++++++++ 3 files changed, 85 insertions(+), 85 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index ee15303c4041..3d6ab2689b5e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4608,7 +4608,16 @@ static struct folio *alloc_swap_folio(struct vm_faul= t *vmf) } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ =20 -static DECLARE_WAIT_QUEUE_HEAD(swapcache_wq); +/* Sanity check that a folio is fully exclusive */ +static void check_swap_exclusive(struct folio *folio, swp_entry_t entry, + unsigned int nr_pages) +{ + /* Called under PT locked and folio locked, the swap count is stable */ + do { + VM_WARN_ON_ONCE_FOLIO(__swap_count(entry) !=3D 1, folio); + entry.val++; + } while (--nr_pages); +} =20 /* * We enter with non-exclusive mmap_lock (to exclude vma changes, @@ -4621,17 +4630,14 @@ static DECLARE_WAIT_QUEUE_HEAD(swapcache_wq); vm_fault_t do_swap_page(struct vm_fault *vmf) { struct vm_area_struct *vma =3D vmf->vma; - struct folio *swapcache, *folio =3D NULL; - DECLARE_WAITQUEUE(wait, current); + struct folio *swapcache =3D NULL, *folio; struct page *page; struct swap_info_struct *si =3D NULL; rmap_t rmap_flags =3D RMAP_NONE; - bool need_clear_cache =3D false; bool exclusive =3D false; softleaf_t entry; pte_t pte; vm_fault_t ret =3D 0; - void *shadow =3D NULL; int nr_pages; unsigned long page_idx; unsigned long address; @@ -4702,57 +4708,21 @@ 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); - swapcache =3D folio; - if (!folio) { - if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && - __swap_count(entry) =3D=3D 1) { - /* skip swapcache */ + if (data_race(si->flags & SWP_SYNCHRONOUS_IO)) { folio =3D alloc_swap_folio(vmf); if (folio) { - __folio_set_locked(folio); - __folio_set_swapbacked(folio); - - nr_pages =3D folio_nr_pages(folio); - if (folio_test_large(folio)) - entry.val =3D ALIGN_DOWN(entry.val, nr_pages); /* - * Prevent parallel swapin from proceeding with - * the cache flag. Otherwise, another thread - * may finish swapin first, free the entry, and - * swapout reusing the same entry. It's - * undetectable as pte_same() returns true due - * to entry reuse. + * folio is charged, so swapin can only fail due + * to raced swapin and return NULL. */ - if (swapcache_prepare(entry, nr_pages)) { - /* - * Relax a bit to prevent rapid - * repeated page faults. - */ - add_wait_queue(&swapcache_wq, &wait); - schedule_timeout_uninterruptible(1); - remove_wait_queue(&swapcache_wq, &wait); - goto out_page; - } - need_clear_cache =3D true; - - memcg1_swapin(entry, nr_pages); - - shadow =3D swap_cache_get_shadow(entry); - if (shadow) - workingset_refault(folio, shadow); - - folio_add_lru(folio); - - /* To provide entry to swap_read_folio() */ - folio->swap =3D entry; - swap_read_folio(folio, NULL); - folio->private =3D NULL; + swapcache =3D swapin_folio(entry, folio); + if (swapcache !=3D folio) + folio_put(folio); + folio =3D swapcache; } } else { - folio =3D swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, - vmf); - swapcache =3D folio; + folio =3D swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vmf); } =20 if (!folio) { @@ -4774,6 +4744,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); } =20 + swapcache =3D folio; ret |=3D folio_lock_or_retry(folio, vmf); if (ret & VM_FAULT_RETRY) goto out_release; @@ -4843,24 +4814,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) goto out_nomap; } =20 - /* allocated large folios for SWP_SYNCHRONOUS_IO */ - if (folio_test_large(folio) && !folio_test_swapcache(folio)) { - unsigned long nr =3D folio_nr_pages(folio); - unsigned long folio_start =3D ALIGN_DOWN(vmf->address, nr * PAGE_SIZE); - unsigned long idx =3D (vmf->address - folio_start) / PAGE_SIZE; - pte_t *folio_ptep =3D vmf->pte - idx; - pte_t folio_pte =3D ptep_get(folio_ptep); - - if (!pte_same(folio_pte, pte_move_swp_offset(vmf->orig_pte, -idx)) || - swap_pte_batch(folio_ptep, nr, folio_pte) !=3D nr) - goto out_nomap; - - page_idx =3D idx; - address =3D folio_start; - ptep =3D folio_ptep; - goto check_folio; - } - nr_pages =3D 1; page_idx =3D 0; address =3D vmf->address; @@ -4904,12 +4857,37 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) BUG_ON(!folio_test_anon(folio) && folio_test_mappedtodisk(folio)); BUG_ON(folio_test_anon(folio) && PageAnonExclusive(page)); =20 + /* + * If a large folio already belongs to anon mapping, then we + * can just go on and map it partially. + * If not, with the large swapin check above failing, the page table + * have changed, so sub pages might got charged to the wrong cgroup, + * or even should be shmem. So we have to free it and fallback. + * Nothing should have touched it, both anon and shmem checks if a + * large folio is fully appliable before use. + * + * This will be removed once we unify folio allocation in the swap cache + * layer, where allocation of a folio stabilizes the swap entries. + */ + if (!folio_test_anon(folio) && folio_test_large(folio) && + nr_pages !=3D folio_nr_pages(folio)) { + if (!WARN_ON_ONCE(folio_test_dirty(folio))) + swap_cache_del_folio(folio); + goto out_nomap; + } + /* * Check under PT lock (to protect against concurrent fork() sharing * the swap entry concurrently) for certainly exclusive pages. */ if (!folio_test_ksm(folio)) { + /* + * The can_swapin_thp check above ensures all PTE have + * same exclusiveness. Checking just one PTE is fine. + */ exclusive =3D pte_swp_exclusive(vmf->orig_pte); + if (exclusive) + check_swap_exclusive(folio, entry, nr_pages); if (folio !=3D swapcache) { /* * We have a fresh page that is not exposed to the @@ -4987,18 +4965,16 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) vmf->orig_pte =3D pte_advance_pfn(pte, page_idx); =20 /* ksm created a completely new copy */ - if (unlikely(folio !=3D swapcache && swapcache)) { + if (unlikely(folio !=3D swapcache)) { folio_add_new_anon_rmap(folio, vma, address, RMAP_EXCLUSIVE); folio_add_lru_vma(folio, vma); } else if (!folio_test_anon(folio)) { /* - * We currently only expect small !anon folios which are either - * fully exclusive or fully shared, or new allocated large - * folios which are fully exclusive. If we ever get large - * folios within swapcache here, we have to be careful. + * We currently only expect !anon folios that are fully + * mappable. See the comment after can_swapin_thp above. */ - VM_WARN_ON_ONCE(folio_test_large(folio) && folio_test_swapcache(folio)); - VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); + VM_WARN_ON_ONCE_FOLIO(folio_nr_pages(folio) !=3D nr_pages, folio); + VM_WARN_ON_ONCE_FOLIO(folio_mapped(folio), folio); folio_add_new_anon_rmap(folio, vma, address, rmap_flags); } else { folio_add_anon_rmap_ptes(folio, page, nr_pages, vma, address, @@ -5038,12 +5014,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); out: - /* Clear the swap cache pin for direct swapin after PTL unlock */ - if (need_clear_cache) { - swapcache_clear(si, entry, nr_pages); - if (waitqueue_active(&swapcache_wq)) - wake_up(&swapcache_wq); - } if (si) put_swap_device(si); return ret; @@ -5051,6 +5021,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); out_page: + if (folio_test_swapcache(folio)) + folio_free_swap(folio); folio_unlock(folio); out_release: folio_put(folio); @@ -5058,11 +5030,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio_unlock(swapcache); folio_put(swapcache); } - if (need_clear_cache) { - swapcache_clear(si, entry, nr_pages); - if (waitqueue_active(&swapcache_wq)) - wake_up(&swapcache_wq); - } if (si) put_swap_device(si); return ret; diff --git a/mm/swap.h b/mm/swap.h index 0fff92e42cfe..214e7d041030 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -268,6 +268,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); void swap_update_readahead(struct folio *folio, struct vm_area_struct *vma, unsigned long addr); =20 @@ -386,6 +387,11 @@ 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) +{ + return NULL; +} + static inline void swap_update_readahead(struct folio *folio, struct vm_area_struct *vma, unsigned long addr) { diff --git a/mm/swap_state.c b/mm/swap_state.c index a8511ce43242..8c429dc33ca9 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -545,6 +545,33 @@ struct folio *swap_cache_alloc_folio(swp_entry_t entry= , gfp_t gfp_mask, return result; } =20 +/** + * swapin_folio - swap-in one or multiple entries skipping readahead. + * @entry: starting swap entry to swap in + * @folio: a new allocated and charged 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 + * with the folio size. + * + * Return: returns pointer to @folio on success. If folio is a large folio + * and this raced with another swapin, NULL will be returned to allow fall= back + * 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 *swapcache; + pgoff_t offset =3D swp_offset(entry); + unsigned long nr_pages =3D folio_nr_pages(folio); + + entry =3D swp_entry(swp_type(entry), round_down(offset, nr_pages)); + swapcache =3D __swap_cache_prepare_and_add(entry, folio, 0, true, false); + if (swapcache =3D=3D folio) + swap_read_folio(folio, NULL); + return swapcache; +} + /* * Locate a page of swap in physical memory, reserving swap cache space * and reading the disk if it is not already cached. --=20 2.52.0