From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) (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 8AFBF22D9ED for ; Sun, 16 Nov 2025 18:12:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763316728; cv=none; b=Ai0xiVS84hDcNsueL9phRa9ppJ1zSFTewtc6ThfXuXPFaQFATPPkXoxz7u3mv//4EUWrHCu/ixNr5AQw34zpTDx/xVYgvFbseB9EnGyTdoMzQBSdaZsbTR+Ub4ObLVtXdFEd5zXS32fcDntg1/XNl4Txeico2pZNlzm58VNthZo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763316728; c=relaxed/simple; bh=8v2eoHjzFxUuEtEC3DH3FxJs/C+oQHBy9NKUp9ifD94=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NPQGf2R4EYaAwl3K0hR5I2ZN6zha5y05YI7JY+qsc5qdPgywv7aA//eWcyl9leXVIKW4h2toE38bHtLvE1fW5EDlHHsX/3hG1r9qW00Q3fEKZs4NlJANkebY4Zo7OcqtbEi9gEHnN1aPR6asU6JUCGGOtYGgCDOJLNjGIc0Nh7o= 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=Cv81dgrc; arc=none smtp.client-ip=209.85.222.171 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="Cv81dgrc" Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-8a3eac7ca30so232806485a.2 for ; Sun, 16 Nov 2025 10:12:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763316725; x=1763921525; 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=NhZ6AXP7RAGh9uysgG1UlJvSzAeXVZ3It6HsfrK2kbA=; b=Cv81dgrcgaDwYK84+4MHNpiEeYuIeppsrdTYBsIgynpO+bzrM+gK3UwEXRRF/bSE9r nHoRB3xP3jd/MKdyqPUnl6jd/6n08Yi2jdg5zgEcXIu7Fmg293ESzGW5dSY+Dz1Tz2mR fR4RbsOL84ef1xJgUoaSN9HVdYbsYNZF6GeK/QHPL9tBFlCfz0dyqaD6TPD92lpNlnzO Pc87B1FdaiDKWdp3QVRmAHrgkDzWukK3LnaSmzkzCxHVlJ3RJ2d5svQQPxXgZnbaMhgs m1pZzhcKe5LA0dcg2iv00nqin0MuwqgrewjyXxcpROPzzLDTitL99IfdktWqmU1K2IrY Z/MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763316725; x=1763921525; 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=NhZ6AXP7RAGh9uysgG1UlJvSzAeXVZ3It6HsfrK2kbA=; b=E2cfs3MK7KMZRS3y1IluVIbBM2VQMyLLd3/QzNikClCbYbOOMxl7b+2zYUUskWbhae sTqfnQw3qk0w8ehX/D7umruNav2QhVEqljBCQCO+Za21caystennLroOvov2qzZRrvJO vHi4kLTiHg3rMRR0MCm7axzdtScqo9Vnm40egrkL982miw2M+KQgQnhiaqyFO9bLN3eF El5sGpVem3frOKwwudlqMVCg3f0/UCrRsGJ6T2k2Q/CZz0c7CODPcujB4e4ttoSdPFXY 31upfkdop5j/Psj3i0qoPLW7pPi7BL4GKb537PjaDdd3jZEIkjpGNtRgqiM578JekXPU crfg== X-Forwarded-Encrypted: i=1; AJvYcCUrLETCYizJRFWT9uyEfrLptglIPajMUyYU5gcGPFvJKacuMO7Y57Ok6rdaX4bokEC38DcfUHoU8ym/2PQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yzo4cay43lPmr5xE8MZiK20lw7jeP/sj2+Qdr3Xuy9U9+xjFmRY fjgO4uu+7qay42JO+WwKx5oNlOX17Z0/YSG4bNyRJUx+OGRfXGlAlOtC X-Gm-Gg: ASbGnctFZT3O2uvQbzCq6TT8OYkxqTfZH1QxxV361unoKagtD2gMxx1s5msnFfragFX OajlGSAj+FkR2wBiHIDREE1swuW+wB/3c+uiNeqPCboeH+WeEyKvQrWfyMcBKLjU4zmi/RIaObr Hk+Xm7qa1F0SK0J+uKLtSTNbjJVKmOtgm3ZU5r2Jl+ltnDEjVEgQfndh+d287u2OOQzly7N9+qX SJX5UDj+R9zZ+rRlMd/5Dz4JA0lUC1t/+EchmJzB/OhKUTL9XGvkpKg6mIWss6QCHxTqcUPP4F1 rjpd3ARAViV6GDcgABYDEgWqBcr3DsWq/aKiF3IY31dRSSdVuUgsOmlVwtjmCWiPNOVGi0MPRkd CneRMgdIjCQ5k8uP52oiJFfy8XyhR9T4/3/xyjiSTpewHWgVjymRcEbxbcWvAoELbfknwm/Js9z cdKhg71oR9srtdVmFBzfqFlu5paMQdSwUOPqIoBLIQ+eLSVowjvT7B X-Google-Smtp-Source: AGHT+IHAWu7Bk+kJ0k1Ixq1Qlx5fg/HwpueJA+4PmIS00LyzPvK6x+eQvNc97iR3Uswub2L27vjJpA== X-Received: by 2002:a05:620a:3952:b0:8b2:6ac5:bce1 with SMTP id af79cd13be357-8b2c31a1308mr1194879185a.52.1763316725248; Sun, 16 Nov 2025 10:12:05 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8b2e089b436sm305447785a.45.2025.11.16.10.11.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Nov 2025 10:12:04 -0800 (PST) From: Kairui Song Date: Mon, 17 Nov 2025 02:11:42 +0800 Subject: [PATCH v2 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: 7bit Message-Id: <20251117-swap-table-p2-v2-1-37730e6ea6d5@tencent.com> References: <20251117-swap-table-p2-v2-0-37730e6ea6d5@tencent.com> In-Reply-To: <20251117-swap-table-p2-v2-0-37730e6ea6d5@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=1763316712; l=8749; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=LgE5Sjz+i9PJ11iVTTGTSw2inPWKcMkfPhBUgoQ/uFw=; b=ovbNvY8byXskgOsqWOv6d+dwDbXMvfs540wcXGorq+nFkeoYa8Psd0shfST/jWpJD2BymxDeQ 2LLVgc00VXGBXdQfxqvI9CjDbGyQisHLuJjqb67Hw5M3kDB5ocj+qUW 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 Signed-off-by: Kairui Song --- mm/swap.h | 6 +++--- mm/swap_state.c | 50 +++++++++++++++++++++++++++++++++----------------- mm/swapfile.c | 2 +- mm/zswap.c | 4 ++-- 4 files changed, 39 insertions(+), 23 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 **shadow); 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_entry_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 b2230f8a48fc..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, } } -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 cache. + * @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 locks. + * 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 = __swap_entry_to_info(entry); struct folio *folio; @@ -452,12 +472,12 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, goto put_and_return; /* - * 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); @@ -509,10 +529,6 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, * and reading the disk if it is not already cached. * A failure return means that either the page allocation failed or that * the swap entry is no longer in use. - * - * get/put_swap_device() aren't needed to call this function, because - * __read_swap_cache_async() call them and swap_read_folio() holds the - * swap cache folio lock. */ struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct vm_area_struct *vma, unsigned long addr, @@ -529,7 +545,7 @@ struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, return NULL; mpol = get_vma_policy(vma, addr, 0, &ilx); - folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, + folio = swap_cache_alloc_folio(entry, gfp_mask, mpol, ilx, &page_allocated, false); mpol_cond_put(mpol); @@ -647,9 +663,9 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, blk_start_plug(&plug); for (offset = start_offset; offset <= end_offset ; offset++) { /* Ok, do the async read-ahead now */ - folio = __read_swap_cache_async( - swp_entry(swp_type(entry), offset), - gfp_mask, mpol, ilx, &page_allocated, false); + folio = swap_cache_alloc_folio( + swp_entry(swp_type(entry), offset), gfp_mask, mpol, ilx, + &page_allocated, false); if (!folio) continue; if (page_allocated) { @@ -666,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 = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, + folio = swap_cache_alloc_folio(entry, gfp_mask, mpol, ilx, &page_allocated, false); if (unlikely(page_allocated)) swap_read_folio(folio, NULL); @@ -771,7 +787,7 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, if (!si) continue; } - folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, + folio = swap_cache_alloc_folio(entry, gfp_mask, mpol, ilx, &page_allocated, false); if (si) put_swap_device(si); @@ -793,7 +809,7 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, lru_add_drain(); skip: /* The folio was likely read above, so no need for plugging here */ - folio = __read_swap_cache_async(targ_entry, gfp_mask, mpol, targ_ilx, + folio = 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 94e0f0c54168..a2bb09867f1e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1573,7 +1573,7 @@ static unsigned char swap_entry_put_locked(struct swap_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; mpol = get_task_policy(current); - folio = __read_swap_cache_async(swpentry, GFP_KERNEL, mpol, - NO_INTERLEAVE_INDEX, &folio_was_allocated, true); + folio = swap_cache_alloc_folio(swpentry, GFP_KERNEL, mpol, + NO_INTERLEAVE_INDEX, &folio_was_allocated, true); put_swap_device(si); if (!folio) return -ENOMEM; -- 2.51.2