From nobody Wed Dec 17 10:57:08 2025 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (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 7D32D1802DD for ; Wed, 8 Jan 2025 02:16:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736302621; cv=none; b=tklxjYrOoQEvoqU/qCa7zcqTEo1fYc9uxpCWMyrUsol6Fde25oDkK5pRaJEg+5MmXXJrJoOTurp6NuptThjvmyfUhzWLgx9jvjvJdEzexDvyuKzxQZiTApQE5ur0srKTKXnjUtr4/X5H4rm8P+cchOUeWgoYdkSbSHvaudXdYRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736302621; c=relaxed/simple; bh=Ns6bnrH9Yq02KfPVld/TR2FMc3EcPJPsRKSJKjUrmDY=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=WS7k2+/+j0SlrMw54GVxGBn8xYS59opU5AEKMbOekvdRAnyvcDwwE5rTzQNmxWHOOjLKDVDoFL6/DAmlXpMMcTZeB5ViTpDlpMHn4D2X8COMu0vN30aaWg4nbtZeoWop9YpOhciBH5/vPz8d24YxZmSwNl51Cp6oTONO5JP7WCA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=eo6X+1g8; arc=none smtp.client-ip=115.124.30.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="eo6X+1g8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1736302616; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=wUuzRVpwJXkcpzpsBXo1F6rLZRwwTYJ3qDWkskJWgY0=; b=eo6X+1g8TDATKOVTdoGzLFLUJgIH1BqURP50t8pw4S/E1U7CU71CZhgpnjmwyJhffJ8xsS9lYqwbWFKjofqVmOrHVhpA+Vn6koqhKzbUDaAlTIBuQUhQPI63xBPWi7lL+GyMhkUywV0k4tH9qOBx7mYwnO653CS6TYksNxSJcvs= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WNCACmM_1736302613 cluster:ay36) by smtp.aliyun-inc.com; Wed, 08 Jan 2025 10:16:54 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, wangkefeng.wang@huawei.com, kasong@tencent.com, ying.huang@linux.alibaba.com, 21cnbao@gmail.com, ryan.roberts@arm.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] mm: shmem: skip swapcache for swapin of synchronous swap device Date: Wed, 8 Jan 2025 10:16:49 +0800 Message-Id: <3d9f3bd3bc6ec953054baff5134f66feeaae7c1e.1736301701.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With fast swap devices (such as zram), swapin latency is crucial to applica= tions. For shmem swapin, similar to anonymous memory swapin, we can skip the swapc= ache operation to improve swapin latency. Testing 1G shmem sequential swapin wit= hout THP enabled, I observed approximately a 6% performance improvement: (Note: I repeated 5 times and took the mean data for each test) w/o patch w/ patch changes 534.8ms 501ms +6.3% In addition, currently, we always split the large swap entry stored in the shmem mapping during shmem large folio swapin, which is not perfect, especi= ally with a fast swap device. We should swap in the whole large folio instead of splitting the precious large folios to take advantage of the large folios and improve the swapin latency if the swap device is synchronous device, which is similar to anonymous memory mTHP swapin. Testing 1G shmem sequenti= al swapin with 64K mTHP and 2M mTHP, I observed obvious performance improvemen= t: mTHP=3D64K w/o patch w/ patch changes 550.4ms 169.6ms +69% mTHP=3D2M w/o patch w/ patch changes 542.8ms 126.8ms +77% Note that skipping swapcache requires attention to concurrent swapin scenar= ios. Fortunately the swapcache_prepare() and shmem_add_to_page_cache() can help identify concurrent swapin and large swap entry split scenarios, and return -EEXIST for retry. Signed-off-by: Baolin Wang --- Changes from v2: - Wrap at 80 columns for function definition, per Matthew. Changes from v1: - Skip calling delete_from_swap_cache() in shmem_set_folio_swapin_error() when skipping the swapcache. --- mm/shmem.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 107 insertions(+), 5 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 95b80c24f6f9..8c9e0e7408b7 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1967,6 +1967,67 @@ static struct folio *shmem_alloc_and_add_folio(struc= t vm_fault *vmf, return ERR_PTR(error); } =20 +static struct folio *shmem_swap_alloc_folio(struct inode *inode, + struct vm_area_struct *vma, pgoff_t index, + swp_entry_t entry, int order, gfp_t gfp) +{ + struct shmem_inode_info *info =3D SHMEM_I(inode); + struct folio *new; + void *shadow; + int nr_pages; + + /* + * We have arrived here because our zones are constrained, so don't + * limit chance of success by further cpuset and node constraints. + */ + gfp &=3D ~GFP_CONSTRAINT_MASK; +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (order > 0) { + gfp_t huge_gfp =3D vma_thp_gfp_mask(vma); + + gfp =3D limit_gfp_mask(huge_gfp, gfp); + } +#endif + + new =3D shmem_alloc_folio(gfp, order, info, index); + if (!new) + return ERR_PTR(-ENOMEM); + + nr_pages =3D folio_nr_pages(new); + if (mem_cgroup_swapin_charge_folio(new, vma ? vma->vm_mm : NULL, + gfp, entry)) { + folio_put(new); + return ERR_PTR(-ENOMEM); + } + + /* + * Prevent parallel swapin from proceeding with the swap cache flag. + * + * Of course there is another possible concurrent scenario as well, + * that is to say, the swap cache flag of a large folio has already + * been set by swapcache_prepare(), while another thread may have + * already split the large swap entry stored in the shmem mapping. + * In this case, shmem_add_to_page_cache() will help identify the + * concurrent swapin and return -EEXIST. + */ + if (swapcache_prepare(entry, nr_pages)) { + folio_put(new); + return ERR_PTR(-EEXIST); + } + + __folio_set_locked(new); + __folio_set_swapbacked(new); + new->swap =3D entry; + + mem_cgroup_swapin_uncharge_swap(entry, nr_pages); + shadow =3D get_shadow_from_swap_cache(entry); + if (shadow) + workingset_refault(new, shadow); + folio_add_lru(new); + swap_read_folio(new, NULL); + return new; +} + /* * When a page is moved from swapcache to shmem filecache (either by the * usual swapin of shmem_get_folio_gfp(), or by the less common swapoff of @@ -2070,7 +2131,8 @@ static int shmem_replace_folio(struct folio **foliop,= gfp_t gfp, } =20 static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t inde= x, - struct folio *folio, swp_entry_t swap) + struct folio *folio, swp_entry_t swap, + bool skip_swapcache) { struct address_space *mapping =3D inode->i_mapping; swp_entry_t swapin_error; @@ -2086,7 +2148,8 @@ static void shmem_set_folio_swapin_error(struct inode= *inode, pgoff_t index, =20 nr_pages =3D folio_nr_pages(folio); folio_wait_writeback(folio); - delete_from_swap_cache(folio); + if (!skip_swapcache) + delete_from_swap_cache(folio); /* * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks * won't be 0 when inode is released and thus trigger WARN_ON(i_blocks) @@ -2190,6 +2253,7 @@ static int shmem_swapin_folio(struct inode *inode, pg= off_t index, struct shmem_inode_info *info =3D SHMEM_I(inode); struct swap_info_struct *si; struct folio *folio =3D NULL; + bool skip_swapcache =3D false; swp_entry_t swap; int error, nr_pages; =20 @@ -2211,6 +2275,8 @@ static int shmem_swapin_folio(struct inode *inode, pg= off_t index, /* Look it up and read it in.. */ folio =3D swap_cache_get_folio(swap, NULL, 0); if (!folio) { + int order =3D xa_get_order(&mapping->i_pages, index); + bool fallback_order0 =3D false; int split_order; =20 /* Or update major stats only when swapin succeeds?? */ @@ -2220,6 +2286,33 @@ static int shmem_swapin_folio(struct inode *inode, p= goff_t index, count_memcg_event_mm(fault_mm, PGMAJFAULT); } =20 + /* + * If uffd is active for the vma, we need per-page fault + * fidelity to maintain the uffd semantics, then fallback + * to swapin order-0 folio, as well as for zswap case. + */ + if (order > 0 && ((vma && unlikely(userfaultfd_armed(vma))) || + !zswap_never_enabled())) + fallback_order0 =3D true; + + /* Skip swapcache for synchronous device. */ + if (!fallback_order0 && data_race(si->flags & SWP_SYNCHRONOUS_IO)) { + folio =3D shmem_swap_alloc_folio(inode, vma, index, swap, order, gfp); + if (!IS_ERR(folio)) { + skip_swapcache =3D true; + goto alloced; + } + + /* + * Fallback to swapin order-0 folio unless the swap entry + * already exists. + */ + error =3D PTR_ERR(folio); + folio =3D NULL; + if (error =3D=3D -EEXIST) + goto failed; + } + /* * Now swap device can only swap in order 0 folio, then we * should split the large swap entry stored in the pagecache @@ -2250,9 +2343,10 @@ static int shmem_swapin_folio(struct inode *inode, p= goff_t index, } } =20 +alloced: /* We have to do this with folio locked to prevent races */ folio_lock(folio); - if (!folio_test_swapcache(folio) || + if ((!skip_swapcache && !folio_test_swapcache(folio)) || folio->swap.val !=3D swap.val || !shmem_confirm_swap(mapping, index, swap)) { error =3D -EEXIST; @@ -2288,7 +2382,12 @@ static int shmem_swapin_folio(struct inode *inode, p= goff_t index, if (sgp =3D=3D SGP_WRITE) folio_mark_accessed(folio); =20 - delete_from_swap_cache(folio); + if (skip_swapcache) { + folio->swap.val =3D 0; + swapcache_clear(si, swap, nr_pages); + } else { + delete_from_swap_cache(folio); + } folio_mark_dirty(folio); swap_free_nr(swap, nr_pages); put_swap_device(si); @@ -2299,8 +2398,11 @@ static int shmem_swapin_folio(struct inode *inode, p= goff_t index, if (!shmem_confirm_swap(mapping, index, swap)) error =3D -EEXIST; if (error =3D=3D -EIO) - shmem_set_folio_swapin_error(inode, index, folio, swap); + shmem_set_folio_swapin_error(inode, index, folio, swap, + skip_swapcache); unlock: + if (skip_swapcache) + swapcache_clear(si, swap, folio_nr_pages(folio)); if (folio) { folio_unlock(folio); folio_put(folio); --=20 2.39.3