From nobody Tue Dec 2 00:46:01 2025 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (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 536772D77E5 for ; Mon, 24 Nov 2025 19:15:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764011744; cv=none; b=iV4Z8Q/ZR8oYenGG8FFUesFKAXbznuuBIAZc5HnYrVxj9zQ5IkkQKKvgnY74OfSECcoC8XenmaDiSCKjR+LiCQy3om39qXDhojjtXKgRiSvsdderG8jzUvfiGRPnihoZMgA0zGLVii0w81FqKiTOMQcsB5s6Adv9r2y21W7wx8g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764011744; c=relaxed/simple; bh=lIFTS89MUmtCEde/402ztDBT0/PXLOgizG5Dlzk5OMs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Jc+MeIhwecM6bqnYyFARHrww/lCpC9birbYmx5ih24A70mMd8Y0YgqXTovkrBCSHYRXjeTIEmQ/+8flBjVOYWLi7huru2GSJjc9Hw9pLot1TKsIFH0cGD9yI9o6zaKmuyMED9OAJHDgmlXPm/rL0sLF8i3lOk2MjPdjDCgUCnz4= 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=cNFFE42H; arc=none smtp.client-ip=209.85.216.50 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="cNFFE42H" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-3437ea05540so4017171a91.0 for ; Mon, 24 Nov 2025 11:15:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764011741; x=1764616541; 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=u9jkj+STmE/k14nh35f8Limla3vrnfgi3c5F8LmHokA=; b=cNFFE42HsszlzjfXghpdOR+QuLVds7yPJlQ5I8bI3VwuexGTIaJbiPRXta3SBPXraA XmYoYU8mX00mE8pnJLDmzKhNXaDCm33an4cUpIoy076uLBjW2yJmNH2tWPwgjLB8fmRJ h7VDHUShNhwKGm7ZxCVm1oxOrU6EseX8hmxebtXXBTnAW9ZF/K0n7icA9OhLqp66pVqv RKQ3s4GY2FtEnxbezF6t0AZRVXuto88V4TK4BhsrZfke5t5wyuhRss6pm7pW7UhQwUdr yryugn2bQgto6P0ZBSYu+oc/hFxzCIOZ50FAwZeENT2QLpSPKLlAObEVECt6GuE5ZhaW WhtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764011741; x=1764616541; 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=u9jkj+STmE/k14nh35f8Limla3vrnfgi3c5F8LmHokA=; b=KBTDdEGRburZnbsXm2DD3Sls3aMtlmhzYPBZSWjaQAZCPng+xzdDh7Cn3czDn68l9S fnGqzOH/Aj1ChRJbVn7q88Z2DmeouUHwfdei70P65Xxq3GBHxB2FMqCZ7TAVnr9muz3x 4LyHK4W0UPhvyjOvL2S5LA7xWcmuk8ZGDZKKrfWSbAHUwp523OhBfhtgGeOngGA+My9J xgYWiPpKld9umNFKj6k5d0VAEMO+GJ9K0kFZS+EdRTIOHzP7HClt9mkVfwMS6e6pVSJl cahoSEXv921l/c24p8SYt9PqKZVcIRgiiMbmC0YIxIb+hRNIGUGg/eb00N/kfOaVtfyJ dmHw== X-Forwarded-Encrypted: i=1; AJvYcCWOFffllwbdIzob4XddAjDB+MI6YpzT4JfShdqz4vRV70RXv/Fu7ZzBGR1JdnLKKQhBKz//bYztXz+5y/E=@vger.kernel.org X-Gm-Message-State: AOJu0YzW2XQURKGQ5mvDdFKCB/kmT+oiQuQih5g/1nmF4HfQXopPfVNB xNl0ZFQF8ooZ87vdKVNBXZK+0WWxVmxKe4LB+lmWbp8UPDKbo1+5Hict X-Gm-Gg: ASbGnctpoXXtnqOy1Tgoda1WffAhFByiUQX3wa3wuDoC+qk8T9HmelL8EaNtmOZqPji kw4sdiuyJt8lXrq/cuFsUQcqZIct+44K0La0sziww87XXG725d9h4Ira3R9uGLZTayrQmNgCSQA lwPb91rYX2B+A0nft+qOrjtk1ASdzharBroc+UpWKZjx6e0mWRQPXq1mnG91Y/C0kx4fLZl4g0v bBQMxW4ixUaGCI7uqVhpBDHP4oJSf2e9dM4AMAwGSDrGX2H4Z0ZMvUjw9xwUw9g8uC2DbjPF+2e RYf06kYb4MV4rObPxv74TI+k529AUwcZSPFYi85nxpJ/2lLEqab5fWXWDVT6jWkexkcc15w38H+ aH28t1JIJqIf8TpCJ3JTWU1mwvuw4jmpBTqeQ0k3xZOoq01LHuHmdS4ZYHmwHUOohviD34HNY1+ H+AiBeqjkzHhuPGe07YrajP+3Jk68Iy+maGQW43fs6ZSF6NgLIWdOt5DJ4d9Y= X-Google-Smtp-Source: AGHT+IGGUc01kDS8qREeDUvGKB0ou8GTVkJk9Tagxxza3GasTdHBffRSTXXNoF4aXll3ha9UOleOmw== X-Received: by 2002:a17:90b:1e0e:b0:340:f05a:3ec2 with SMTP id 98e67ed59e1d1-34733f34135mr15093395a91.17.1764011741360; Mon, 24 Nov 2025 11:15:41 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-bd75def75ffsm14327479a12.3.2025.11.24.11.15.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Nov 2025 11:15:40 -0800 (PST) From: Kairui Song Date: Tue, 25 Nov 2025 03:13:44 +0800 Subject: [PATCH v3 01/19] mm, swap: rename __read_swap_cache_async to swap_cache_alloc_folio 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: <20251125-swap-table-p2-v3-1-33f54f707a5c@tencent.com> References: <20251125-swap-table-p2-v3-0-33f54f707a5c@tencent.com> In-Reply-To: <20251125-swap-table-p2-v3-0-33f54f707a5c@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=1764011730; l=8299; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=avBXrzr/6LcA0po0XEbV8MSGPC0ib13hKBO1eBIu8Z8=; b=AClgnDVHupKXe6zKun7ExA0riZ3WusbVWCDk0HLSr8Nw0f6hBl58RDuVlmxlTSApXArTiHjzQ J3478M2xmErDF3a11oYExiZqSy1rvVQWb5rEmF3uqZJrdQtSiI84+tu X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Kairui Song __read_swap_cache_async is widely used to allocate and ensure a folio is in swapcache, or get the folio if a folio is already there. It's not async, and it's not doing any read. Rename it to better present its usage, and prepare to be reworked as part of new swap cache APIs. Also, add some comments for the function. Worth noting that the skip_if_exists argument is an long existing workaround that will be dropped soon. Reviewed-by: Yosry Ahmed Acked-by: Chris Li Reviewed-by: Barry Song Reviewed-by: Nhat Pham Signed-off-by: Kairui Song --- mm/swap.h | 6 +++--- mm/swap_state.c | 46 +++++++++++++++++++++++++++++++++------------- mm/swapfile.c | 2 +- mm/zswap.c | 4 ++-- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/mm/swap.h b/mm/swap.h index d034c13d8dd2..0fff92e42cfe 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -249,6 +249,9 @@ struct folio *swap_cache_get_folio(swp_entry_t entry); void *swap_cache_get_shadow(swp_entry_t entry); void swap_cache_add_folio(struct folio *folio, swp_entry_t entry, void **s= hadow); void swap_cache_del_folio(struct folio *folio); +struct folio *swap_cache_alloc_folio(swp_entry_t entry, gfp_t gfp_flags, + struct mempolicy *mpol, pgoff_t ilx, + bool *alloced, bool skip_if_exists); /* Below helpers require the caller to lock and pass in the swap cluster. = */ void __swap_cache_del_folio(struct swap_cluster_info *ci, struct folio *folio, swp_entry_t entry, void *shadow); @@ -261,9 +264,6 @@ void swapcache_clear(struct swap_info_struct *si, swp_e= ntry_t entry, int nr); struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct vm_area_struct *vma, unsigned long addr, struct swap_iocb **plug); -struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_flags, - struct mempolicy *mpol, pgoff_t ilx, bool *new_page_allocated, - bool skip_if_exists); 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, diff --git a/mm/swap_state.c b/mm/swap_state.c index 5f97c6ae70a2..08252eaef32f 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -402,9 +402,29 @@ void swap_update_readahead(struct folio *folio, struct= vm_area_struct *vma, } } =20 -struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, - struct mempolicy *mpol, pgoff_t ilx, bool *new_page_allocated, - bool skip_if_exists) +/** + * swap_cache_alloc_folio - Allocate folio for swapped out slot in swap ca= che. + * @entry: the swapped out swap entry to be binded to the folio. + * @gfp_mask: memory allocation flags + * @mpol: NUMA memory allocation policy to be applied + * @ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE + * @new_page_allocated: sets true if allocation happened, false otherwise + * @skip_if_exists: if the slot is a partially cached state, return NULL. + * This is a workaround that would be removed shortly. + * + * Allocate a folio in the swap cache for one swap slot, typically before + * doing IO (e.g. swap in or zswap writeback). The swap slot indicated by + * @entry must have a non-zero swap count (swapped out). + * Currently only supports order 0. + * + * Context: Caller must protect the swap device with reference count or lo= cks. + * Return: Returns the existing folio if @entry is cached already. Returns + * NULL if failed due to -ENOMEM or @entry have a swap count < 1. + */ +struct folio *swap_cache_alloc_folio(swp_entry_t entry, gfp_t gfp_mask, + struct mempolicy *mpol, pgoff_t ilx, + bool *new_page_allocated, + bool skip_if_exists) { struct swap_info_struct *si =3D __swap_entry_to_info(entry); struct folio *folio; @@ -452,12 +472,12 @@ struct folio *__read_swap_cache_async(swp_entry_t ent= ry, gfp_t gfp_mask, goto put_and_return; =20 /* - * Protect against a recursive call to __read_swap_cache_async() + * Protect against a recursive call to swap_cache_alloc_folio() * on the same entry waiting forever here because SWAP_HAS_CACHE * is set but the folio is not the swap cache yet. This can * happen today if mem_cgroup_swapin_charge_folio() below * triggers reclaim through zswap, which may call - * __read_swap_cache_async() in the writeback path. + * swap_cache_alloc_folio() in the writeback path. */ if (skip_if_exists) goto put_and_return; @@ -466,7 +486,7 @@ struct folio *__read_swap_cache_async(swp_entry_t entry= , gfp_t gfp_mask, * We might race against __swap_cache_del_folio(), and * stumble across a swap_map entry whose SWAP_HAS_CACHE * has not yet been cleared. Or race against another - * __read_swap_cache_async(), which has set SWAP_HAS_CACHE + * swap_cache_alloc_folio(), which has set SWAP_HAS_CACHE * in swap_map, but not yet added its folio to swap cache. */ schedule_timeout_uninterruptible(1); @@ -525,7 +545,7 @@ struct folio *read_swap_cache_async(swp_entry_t entry, = gfp_t gfp_mask, return NULL; =20 mpol =3D get_vma_policy(vma, addr, 0, &ilx); - folio =3D __read_swap_cache_async(entry, gfp_mask, mpol, ilx, + folio =3D swap_cache_alloc_folio(entry, gfp_mask, mpol, ilx, &page_allocated, false); mpol_cond_put(mpol); =20 @@ -643,9 +663,9 @@ struct folio *swap_cluster_readahead(swp_entry_t entry,= gfp_t gfp_mask, blk_start_plug(&plug); for (offset =3D start_offset; offset <=3D end_offset ; offset++) { /* Ok, do the async read-ahead now */ - folio =3D __read_swap_cache_async( - swp_entry(swp_type(entry), offset), - gfp_mask, mpol, ilx, &page_allocated, false); + folio =3D swap_cache_alloc_folio( + swp_entry(swp_type(entry), offset), gfp_mask, mpol, ilx, + &page_allocated, false); if (!folio) continue; if (page_allocated) { @@ -662,7 +682,7 @@ struct folio *swap_cluster_readahead(swp_entry_t entry,= gfp_t gfp_mask, lru_add_drain(); /* Push any new pages onto the LRU now */ skip: /* The page was likely read above, so no need for plugging here */ - folio =3D __read_swap_cache_async(entry, gfp_mask, mpol, ilx, + folio =3D swap_cache_alloc_folio(entry, gfp_mask, mpol, ilx, &page_allocated, false); if (unlikely(page_allocated)) swap_read_folio(folio, NULL); @@ -767,7 +787,7 @@ static struct folio *swap_vma_readahead(swp_entry_t tar= g_entry, gfp_t gfp_mask, if (!si) continue; } - folio =3D __read_swap_cache_async(entry, gfp_mask, mpol, ilx, + folio =3D swap_cache_alloc_folio(entry, gfp_mask, mpol, ilx, &page_allocated, false); if (si) put_swap_device(si); @@ -789,7 +809,7 @@ static struct folio *swap_vma_readahead(swp_entry_t tar= g_entry, gfp_t gfp_mask, lru_add_drain(); skip: /* The folio was likely read above, so no need for plugging here */ - folio =3D __read_swap_cache_async(targ_entry, gfp_mask, mpol, targ_ilx, + folio =3D swap_cache_alloc_folio(targ_entry, gfp_mask, mpol, targ_ilx, &page_allocated, false); if (unlikely(page_allocated)) swap_read_folio(folio, NULL); diff --git a/mm/swapfile.c b/mm/swapfile.c index d12332423a06..ee6bb37ab174 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1570,7 +1570,7 @@ static unsigned char swap_entry_put_locked(struct swa= p_info_struct *si, * CPU1 CPU2 * do_swap_page() * ... swapoff+swapon - * __read_swap_cache_async() + * swap_cache_alloc_folio() * swapcache_prepare() * __swap_duplicate() * // check swap_map diff --git a/mm/zswap.c b/mm/zswap.c index 5d0f8b13a958..a7a2443912f4 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1014,8 +1014,8 @@ static int zswap_writeback_entry(struct zswap_entry *= entry, return -EEXIST; =20 mpol =3D get_task_policy(current); - folio =3D __read_swap_cache_async(swpentry, GFP_KERNEL, mpol, - NO_INTERLEAVE_INDEX, &folio_was_allocated, true); + folio =3D swap_cache_alloc_folio(swpentry, GFP_KERNEL, mpol, + NO_INTERLEAVE_INDEX, &folio_was_allocated, true); put_swap_device(si); if (!folio) return -ENOMEM; --=20 2.52.0