From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 49628606A336 for ; Thu, 3 Dec 2026 22:00:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=GDsW7rdP Received: by kanga.kvack.org (Postfix) id 5C75F6B05B8; Thu, 3 Dec 2026 17:00:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 59E456B05B9; Thu, 3 Dec 2026 17:00:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 466146B05BA; Thu, 3 Dec 2026 17:00:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id E117E6B05B8 for ; Thu, 3 Dec 2026 17:00:26 -0500 (EST) Received: from smtpin03.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3AA59A6FA6 for ; Thu, 3 Dec 2026 22:00:26 +0000 (UTC) X-FDA: 85504041252.03.79BA68D Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) by imf25.hostedemail.com (Postfix) with ESMTP id 519BDA0019 for ; Thu, 3 Dec 2026 22:00:24 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=GDsW7rdP; spf=pass (imf25.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796335224; b=r6FGipECrzFZgvywWZNdVEjQ29cIEUzj3ITEHwU6MoClvUQZYqPy6z0cz3Xetfh8CEVIiT uLdtAL0sorLqXokvryjtxKuENfZODz3FsiACDyM1CEUW60ZrqrUp5Nx2xYTX+5up9h61fp V4KSZGMFb6s2KEJtJAHR/WVnNmmp5XQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796335224; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=EcI0k9l+i0RrB5mb15Me4TNV7F2nbc4miUVUIy4Sn3A=; b=2J91NNi9Emtp5Y0sELKhzlVj12wmhLeDdK82YcAXofyLh/gzPzLyB3kprrGNLSvt+xO9kC W2enrsgreQoZrpn5o2qLAcK3GOLIFc1Fg4dMqa6hcX8o3vfRpvfToJv0D8edmpBkXdvYI7 S4xdnOOVbnBeKKHT5RgZ75ks6N6GmYE= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=GDsW7rdP; spf=pass (imf25.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-8b4fb5d96b3so5348827b3.1 for ; Thu, 03 Dec 2026 14:00:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335223; x=1796940023; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=EcI0k9l+i0RrB5mb15Me4TNV7F2nbc4miUVUIy4Sn3A=; b=GDsW7rdPYep0XtzudxoCifFsvooLgMaM6tcgJE6pYOQffb1F/+WOoaWJTo7rejq/ph RuW4NVWSU6dOyn3hQBoEilHDO5Luel89DGI/3tlR9Y2Yd2FbxmWkS3Ed+m5NvfCuuBwH Lqh8xd5pdOCpz5XTz6JYAm0dst3JvsiI0z6ipmlzf7JlaLTtUpGQP5EFjk/hMvbWvbO0 j3zxrE/1TABN6vPvcF6tP4FGdHLFbKYLEZ3bEwwgsV5pGpywZPephKbfw5oYb9b+qIk9 TrHB0Pqa9ayuTPVToElGhoxhG0w/2+bTJJLuToLUwCtG2SBIIwg3GjPDVcXH+3HBgKR7 BO8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335223; x=1796940023; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=EcI0k9l+i0RrB5mb15Me4TNV7F2nbc4miUVUIy4Sn3A=; b=DOtjW9BwgKNzJThJ96zJ4tO4n+rHtnUOj6kb1kZ4WJwiBqtUplO4fhfA/agjJcXPWU MTsQnbeGv+whFKLSp+49bocLs7wvB6i3QFB6px7tKl1nw3sK8WuKtCZRook/w1isg0C+ PyZaMzCL1rfT4eGIuz66qPV0XbTjgSzGCtuOnI/RAT9skgxMWWwVpNa7H2HfkX4Soo6j RfbOWOiABKMPhZrRX8+Tueny1rAa7qxL/J3pWUnUDRFKAmOJkc4yf+Tc35Kzd1qs+/s8 +uJX4EgiiezzcK8Irlt1o2ThUY5TUYXTOeE9MEQpbjCApPlNFu43WhG9q0rqONR0zrQs NdfQ== X-Gm-Message-State: APuU4Z88MZTm6Ix93g6yVNLYyPYAXclDet2ttLv5RiAYiudTMsCsZ/KX xQcUvahWXSFMKPSrT94LJ3AEcGN3+tWG0LUK9nSmtrQ+aKPhT8mxJdX86OErSeuD X-Gm-Gg: AZFdTZfsM3m0cMDXT+FO0Mm9n5rTZDDK7VP2OL9DhS2BXPqYUgzQoGbLjTmsMswD4qr PdDVofcqeEPHCy2nqmC22TIEpoFSLeaAQhB6a0qZNNCH7bBiX7j5xV0HkrvGnKW7WNV8tNQHGZT VYRbm6Ra3+p8d49i2dYGNSsA3hoDwN5ztVIrZCnWbhBtW3daRGkasw6bMuuIWgI04f5gCYgzRjM DrJwNvqoSkoGqud5jHSRljkIgvBpJobkQofOo6mXQSSkgDxhh9ZH536LO/AgCdyb+jgrMEWhwp8 kfU0eZujOxq5x2ozew00kMeX8ecC2+xkaZ8lWPUjquS2axSzrt3kVZMyD5AHYH4nlv/8oM/UeLU 93q5HNlUQDT+ZUxIGFHeV9RLU7gURWn3ywFPaE9LPuLmvnH8/iye+yzgUpSSh+g4JP4Bjmhj+fy ts9PPth0LQWg6kFp0DI4a0vVyRpHk24gUHTFI0Lwgti0JHhPELStDy4G0hVHBt8FdbAe8eZ9hwQ ldV2yqZ0nz+CGTW0ma7gSB7wIZAmxPhOgOKAy1xLb1wdgH9PrLuRhpTBDud1E9WlpvlwfixbQG5 MInm/EXkypA0RUjaT/mbQWT8/dFmXQvvdftc7vDf+s1Ekis/HsTM9t0dIji4z3xbE/UxAoJ63Hw +ytb991jzNOrYtT6q6sjLBqDlkVR8TcAc4WKY X-Received: by 2002:a05:690c:288:b0:8b5:4dbe:283b with SMTP id 00721157ae682-8b5f596ef09mr17776017b3.4.1796335223004; Thu, 03 Dec 2026 14:00:23 -0800 (PST) Received: from localhost ([2a03:2880:10ff:1::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b66086625dsm1054747b3.14.2026.12.03.14.00.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:22 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Baoquan He , Barry Song , Youngjun Park , Johannes Weiner , Yosry Ahmed , Chengming Zhou , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baolin Wang , Michal Hocko , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Hugh Dickins , Jonathan Corbet , Shuah Khan , Randy Dunlap , Roman Gushchin , Muchun Song , Arnd Bergmann , Karl Mehltretter , Dapeng Mi , Marco Elver , Ethan Nelson-Moore , "Borislav Petkov (AMD)" , Jeff Layton , linux-doc@vger.kernel.org, cgroups@vger.kernel.org, Tejun Heo , =?UTF-8?q?Michal=20Koutn=C3=BD?= Subject: [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags Date: Thu, 3 Dec 2026 13:59:56 -0800 Message-ID: <20261203220010.1302207-7-nphamcs@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 519BDA0019 X-Stat-Signature: x881n7hp8h5fz14cyi5hbowri47qnt8o X-Rspam-User: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796335224-104318 X-HE-Meta: U2FsdGVkX1/TEkCaIcQrg6xVZaVhZ/uvWTaijeIpMVJV7kuSo5lo2zWt3UuNWdcqtEmu7joS14V/03JxCEE/5lHnBISwX8wdSWptOfeMUzurKYhrA8EtS2A11vRxk2DENk7+KMB3mEvP0Q+q4On2P5QjRUmDuQ2kzpsxF49dQ3G6TukNvxBzUWufan+LDX6uxF14z+pZ/a0TOtdNHQBKXm8dI8PTbX7dTxChkyy3NZ1fED0qu1dWdIYcsYiI06Woswe8aCI+uYJ3xDM6iZPI8+KbXBIijczr15hKMN47sJ4pzFG7acAUHUmDMy4icMtCgXpRhF42ssAuC/Img1TyHtwSJUwMjIk49Q9KkuwkaLTq9vybeTUEZKE+GdxSDT4I0vMGubrGXcImgkMGenDff8WDJjGul0Tr+dy+3AxOW31pZbVXyMqU61Su4ty5lPOsz+wn62MzQ3ocfS13HYbuM+lXsBP6n/dEtTpWgJcHV34B3YFO9z8I3fPUn5NqE0YLk0SUJbf5ryVuxVht7cpKxYTXUOQ/AeSSO9fjt10SyShor2Pnq+JyPdy1TvUYK44a+8iFowA5UQtItSh+J5dM9tsnpcn/CiMwihIbbQuSh2z2B/mEYia0mBl7qvJZe5NJClUl4xuocHAuSU/nxj0eZlDpoy2QBJ6M+UsDQdx6mSIlWcfcrviRYc3DDBq6NRne1XSYI2aCqsLdGDeXoPyoO6P+aI4uPNavDhfOE0EME4e+SR7deBB2zrvuuxdFASNIXiW+peUKvXLgSIeXEtgQEQ+hnmP+DOj+YMmRS3e7tOTxqf28NlhNKPZ5JMGmFbP1C5LuWVtWqFRMxROZ+06XopJK6qbqrNwXBnNR/ActH+gcAraIeD0zSlAZB33OK+qcPLe04RZuvmd9N7Smhtqb+fNMWh2xL0yHJuWpGNBjiymeSuF1vX5MRj3pWKCz+5Dx/gxEXq5JktgYh65NrEA ajv2G55M PmVuhfUAnrUjIO8au9tXAGJqCraQa4kWachByDdV7Ljq4AnpLYEQqMsTxdjXt1cxgh7KfZLX1n2d03XRzhyql/uZXXBwysY32sCwQSu/lD8qkllGx/mgs3By2s0H2kr3Slr1EHUF7F9Q7EtotaHNCXWJmsWSg1phhFxc9YSzjEpD/ybcHx5Kz3rykXGp3lw1yKjVILvwlIsu4njXEkxNr481tmb2L2BvalnJwo1Augzjh8DJXUTIVsaf4XJa1syEvXlmnkeH3FJdgjoigZFVlql+0KhMz1aVt/ZHMalZLUp7V/nRl4dJBFadUBMqcujdld4HfB+U5S+RSDhXxhi8bf0otnGuSMYiDqmN+ftV4Z1sf6vgTAsY67OVnEC3XrC4eufE5AIDrHqZhU8rjWjhjOYlJXtEX7Ii1x+ZsPxGd6WcYSnSZamhbqPYnFSh0bc02CEG1jnoqm+NSoDxWlMDc4tV6zCZIodgUIAsOglS25hnBZgujlfVxCSOzgnUru/flSJOogpd1tLor+AfLpux7JQMdNlSjFa4dPO5fnwkcprfbokl+TlycWnCtDLzjxEBPFaCfw26y9PmwRe9WePoXWw8kbDOMQ0D6Nf96GsEU1ZieODqW+Em2bwLxINL1qnm2MsBvEHMSQ9Nlxt5NcsnmQ215itqSHQmxVrpJYsRBlUCl4lm/qt6+I5Kcow== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 A virtual entry does not identify the device that holds its disk data. Add physical-entry encoding and lookup to the vtable, and use the lookup for reads, synchronous I/O, stable writes and filesystem-reclaim checks. Prepare these readers before writeout can install physical backing. They must resolve the physical device when the physical backend starts writing virtual folios to disk. Direct physical entries retain their existing device selection. Signed-off-by: Nhat Pham --- mm/memory.c | 15 ++-- mm/page_io.c | 21 +++++- mm/swapfile.c | 2 +- mm/vmscan.c | 23 +++++-- mm/vswap.h | 184 +++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 228 insertions(+), 17 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 9ba6ebd58132..3eb1841530bb 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4754,8 +4754,11 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si, { if (!folio_test_swapcache(folio)) return false; - /* A vswap entry holds no physical slot, so keeping it saves no IO. */ - if (is_vswap_entry(folio->swap)) + /* + * Non-swapfile backends cannot be reused for future swapouts. + * Free the swap slot unless backed by contiguous physical swap. + */ + if (!folio_phys_swap_backed(folio)) return true; /* * Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap @@ -4763,7 +4766,7 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si, * are fast, and meanwhile, swap cache pinning the slot deferring the * release of metadata or fragmentation is a more critical issue. */ - if (data_race(si->flags & SWP_SYNCHRONOUS_IO)) + if (swap_entry_backend_has_flag(si, folio->swap, SWP_SYNCHRONOUS_IO)) return true; if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) || folio_test_mlocked(folio)) @@ -5109,7 +5112,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) swap_update_readahead(folio, vma, vmf->address); if (!folio) { /* Swapin bypasses readahead for SWP_SYNCHRONOUS_IO devices and zswap */ - if (swap_entry_synchronous(si, entry)) + if (swap_entry_synchronous(si, entry) || + (is_vswap_entry(entry) && + swap_entry_backend_has_flag(si, entry, SWP_SYNCHRONOUS_IO))) folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE, thp_swapin_suitable_orders(si, vmf) | BIT(0), vmf, NULL, 0); @@ -5268,7 +5273,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) */ exclusive = true; } else if (exclusive && folio_test_writeback(folio) && - data_race(si->flags & SWP_STABLE_WRITES)) { + swap_entry_backend_has_flag(si, entry, SWP_STABLE_WRITES)) { /* * This is tricky: not all swap backends support * concurrent page modifications while under writeback. diff --git a/mm/page_io.c b/mm/page_io.c index 9397ef83a60d..956c3ffc1039 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -493,17 +493,32 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) struct swap_info_struct *sis = __swap_entry_to_info(folio->swap); bool synchronous = sis->flags & SWP_SYNCHRONOUS_IO; bool workingset = folio_test_workingset(folio); + swp_entry_t phys; VM_BUG_ON_FOLIO(!folio_test_swapcache(folio) && !synchronous, folio); VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(folio_test_uptodate(folio), folio); + /* + * Resolve the physical slot to read from. A vswap entry keeps + * folio->swap virtual, so map it to its physical backing; a folio with + * no backing has nothing to read. + */ + if (swap_is_vswap(sis)) { + phys = vswap_to_phys(folio->swap); + if (phys.val) + synchronous = __swap_entry_to_info(phys)->flags & + SWP_SYNCHRONOUS_IO; + } else { + phys = folio->swap; + } + /* * A batch this folio can't join is submitted before the pressure * window opens, so that each batch is billed for its own read * rather than for the next folio's lookup and decompression. */ - if (ctx->sio && !swap_can_merge(ctx, folio, folio->swap, READ)) + if (ctx->sio && (!phys.val || !swap_can_merge(ctx, folio, phys, READ))) swap_read_submit(ctx); /* @@ -532,14 +547,14 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) if (zswap_load(folio) != -ENOENT) return; - if (unlikely(swap_is_vswap(sis))) { + if (!phys.val) { folio_unlock(folio); return; } /* We have to read from slower devices. Increase zswap protection. */ zswap_folio_swapin(folio); - swap_add_folio(ctx, folio, folio->swap, READ); + swap_add_folio(ctx, folio, phys, READ); /* * Synchronous devices do the whole read at submission time, submit diff --git a/mm/swapfile.c b/mm/swapfile.c index fa315389399b..d9e8c9365c44 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -247,7 +247,7 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, need_reclaim = ((flags & TTRS_ANYWAY) || ((flags & TTRS_UNMAPPED) && !folio_mapped(folio)) || ((flags & TTRS_FULL) && mem_cgroup_swap_full(folio) && - !is_vswap_entry(folio->swap))); + folio_phys_swap_backed(folio))); if (!need_reclaim || !folio_swapcache_freeable(folio)) goto out_unlock; diff --git a/mm/vmscan.c b/mm/vmscan.c index 3ad66f2a16d8..5bf20b57fe45 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -70,6 +70,7 @@ #include "memory-failure.h" #include "page_alloc.h" #include "swap.h" +#include "vswap.h" #define CREATE_TRACE_POINTS #include "trace_vmscan.h" @@ -1029,17 +1030,26 @@ static unsigned int demote_folio_list(struct list_head *demote_folios, static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask) { + swp_entry_t entry; + if (gfp_mask & __GFP_FS) return true; /* * We can "enter_fs" for swap-cache with only __GFP_IO unless backed by * a swapfile that requires GFP_NOFS I/O. */ - if (folio_test_swapcache(folio) && (gfp_mask & __GFP_IO) && - !(__swap_entry_to_info(folio->swap)->ops->flags & - SWAP_OPS_F_REQUIRE_NOFS)) - return true; - return false; + if (!folio_test_swapcache(folio) || !(gfp_mask & __GFP_IO)) + return false; + + entry = folio->swap; + if (is_vswap_entry(entry)) { + entry = vswap_to_phys(entry); + if (!entry.val) + return true; + } + + return !(__swap_entry_to_info(entry)->ops->flags & + SWAP_OPS_F_REQUIRE_NOFS); } /* @@ -1583,7 +1593,8 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, activate_locked: /* Not a candidate for swapping, so reclaim swap space. */ if (folio_test_swapcache(folio) && - (mem_cgroup_swap_full(folio) || folio_test_mlocked(folio))) + ((mem_cgroup_swap_full(folio) && folio_phys_swap_backed(folio)) || + folio_test_mlocked(folio))) folio_free_swap(folio); VM_BUG_ON_FOLIO(folio_test_active(folio), folio); if (!folio_test_mlocked(folio)) { diff --git a/mm/vswap.h b/mm/vswap.h index f64b1543e01f..c4f0670c9b5c 100644 --- a/mm/vswap.h +++ b/mm/vswap.h @@ -19,6 +19,7 @@ struct zswap_entry; enum vswap_backing_type { VSWAP_NONE = 0, VSWAP_ZSWAP = 1, + VSWAP_SWAPFILE = 2, VSWAP_ZERO, VSWAP_FOLIO, }; @@ -41,8 +42,13 @@ static inline bool is_vswap_entry(swp_entry_t entry) /* * Virtual table entry encoding for vswap clusters. * - * Zero represents no separate backing. An aligned zswap pointer - * has bit 0 clear and is stored directly in the vtable. + * The architecture-independent swp_entry_t leaves its top bit unused. + * Shift the entry left by one to leave bit 0 free as the physical tag. + * Aligned zswap_entry pointers already have bit 0 clear. + * + * NONE: |------ 0 -------|0| + * ZSWAP: |-- zswap_entry pointer --| + * SWAPFILE: |-- swp_entry_t -|1| * * vtable[i] = NONE does not by itself mean "free". The swap_table entry * and the per-slot zero flag carry the rest of the state. The full @@ -56,6 +62,8 @@ static inline bool is_vswap_entry(swp_entry_t entry) * NONE | * | set | zero-backed; cached if PFN set * ZSWAP | PFN | clear | folio cached + zswap entry * ZSWAP | shadow / NULL | clear | evicted, only in zswap + * SWAPFILE | PFN | clear | folio cached + physical slot + * SWAPFILE | shadow / NULL | clear | evicted, only on the swapfile * * Locking: the caller must own and lock the slot's swapcache folio to * stabilize its backing. The cluster lock only makes individual vtable @@ -75,9 +83,17 @@ static inline bool is_vswap_entry(swp_entry_t entry) static inline enum vswap_backing_type vtable_type(unsigned long vt) { + if (vt & VTABLE_TAG_MASK) + return VSWAP_SWAPFILE; return vt ? VSWAP_ZSWAP : VSWAP_NONE; } +static inline swp_entry_t vtable_to_phys(unsigned long vt) +{ + VM_WARN_ON(vtable_type(vt) != VSWAP_SWAPFILE); + return (swp_entry_t){ .val = vt >> VTABLE_TAG_BITS }; +} + static inline struct zswap_entry *vtable_to_zswap(unsigned long vt) { VM_WARN_ON(vtable_type(vt) != VSWAP_ZSWAP); @@ -119,6 +135,33 @@ vswap_lock_cluster(swp_entry_t entry, unsigned int *voff) return container_of(ci, struct swap_cluster_info_dynamic, ci); } +/** + * vswap_to_phys - resolve a vswap entry's physical swap backing + * @entry: the virtual swap entry + * + * Context: takes and drops the vswap cluster lock internally. + * Return: the backing physical swp_entry_t, or the null entry (.val == 0) + * when @entry has no physical backing (NONE/ZSWAP/ZERO). + */ +static inline swp_entry_t vswap_to_phys(swp_entry_t entry) +{ + struct swap_cluster_info_dynamic *ci_dyn; + unsigned int voff; + unsigned long vt; + + ci_dyn = vswap_lock_cluster(entry, &voff); + if (!ci_dyn) + return (swp_entry_t){}; + + vt = __vtable_get(ci_dyn, voff); + swap_cluster_unlock(&ci_dyn->ci); + + if (vtable_type(vt) != VSWAP_SWAPFILE) + return (swp_entry_t){}; + + return vtable_to_phys(vt); +} + void __vswap_release_backing(struct swap_cluster_info *ci, unsigned int ci_start, unsigned int nr); @@ -170,6 +213,102 @@ static inline struct zswap_entry *vswap_zswap_load(swp_entry_t entry) void folio_release_vswap_backing(struct folio *folio); +/* + * Walk nr vtable slots starting at voff in ci_dyn. Returns the prefix + * length of slots sharing one effective backing type. For SWAPFILE, + * the prefix is also restricted to contiguous offsets in the same + * swapfile. + * + * Effective type per slot: + * vtable=NONE + zero flag set -> VSWAP_ZERO + * vtable=NONE + swap_table PFN tag -> VSWAP_FOLIO + * vtable=NONE + neither -> VSWAP_NONE + * vtable=SWAPFILE -> VSWAP_SWAPFILE + * vtable=ZSWAP -> VSWAP_ZSWAP + * + * *typep returns the effective type of slot 0. Caller holds + * ci_dyn->ci.lock. + */ +static inline int __vswap_check_backing(struct swap_cluster_info_dynamic *ci_dyn, + unsigned int voff, int nr, + enum vswap_backing_type *typep) +{ + enum vswap_backing_type first_type = VSWAP_NONE; + enum vswap_backing_type slot_type; + swp_entry_t first_phys = {}; + unsigned long vt, swap_tb; + int i; + + lockdep_assert_held(&ci_dyn->ci.lock); + + for (i = 0; i < nr; i++) { + vt = __vtable_get(ci_dyn, voff + i); + if (vtable_type(vt) == VSWAP_NONE) { + swap_tb = __swap_table_get(&ci_dyn->ci, voff + i); + if (__swap_table_test_zero(&ci_dyn->ci, voff + i)) + slot_type = VSWAP_ZERO; + else if (swp_tb_is_folio(swap_tb)) + slot_type = VSWAP_FOLIO; + else + slot_type = VSWAP_NONE; + } else { + slot_type = vtable_type(vt); + } + + if (!i) { + first_type = slot_type; + if (first_type == VSWAP_SWAPFILE) + first_phys = vtable_to_phys(vt); + } else if (slot_type != first_type) { + break; + } else if (first_type == VSWAP_SWAPFILE && + vtable_to_phys(vt).val != first_phys.val + i) { + break; + } + } + + if (typep) + *typep = first_type; + return i; +} + +static inline int vswap_check_backing(swp_entry_t entry, int nr, + enum vswap_backing_type *typep) +{ + struct swap_cluster_info_dynamic *ci_dyn; + unsigned int voff; + int ret; + + ci_dyn = vswap_lock_cluster(entry, &voff); + if (!ci_dyn) { + if (typep) + *typep = VSWAP_NONE; + return 0; + } + ret = __vswap_check_backing(ci_dyn, voff, nr, typep); + swap_cluster_unlock(&ci_dyn->ci); + return ret; +} + +/** + * folio_phys_swap_backed - test whether a folio is backed by a contiguous + * range of physical swap slots. + * @folio: a swap-cache resident folio + * + * Return: %true if @folio->swap is not a vswap entry, or if these vswap + * entries are backed by a contiguous range of physical slots. + */ +static inline bool folio_phys_swap_backed(struct folio *folio) +{ + swp_entry_t entry = folio->swap; + int nr = folio_nr_pages(folio); + enum vswap_backing_type type; + + return !is_vswap_entry(entry) || + (vswap_check_backing(entry, nr, &type) == nr && + type == VSWAP_SWAPFILE); +} + static inline int vswap_cluster_alloc_vtable(struct swap_cluster_info_dynamic *ci_dyn, gfp_t gfp) { @@ -199,6 +338,47 @@ static inline bool is_vswap_entry(swp_entry_t entry) return false; } +static inline swp_entry_t vswap_to_phys(swp_entry_t entry) +{ + return (swp_entry_t){}; +} + +static inline bool folio_phys_swap_backed(struct folio *folio) +{ + return true; +} + #endif /* CONFIG_SWAP */ +/* + * Test a per-backend swap flag (SWP_SYNCHRONOUS_IO, SWP_STABLE_WRITES, ...) + * for @entry. For a vswap entry the property belongs to the current + * physical backing rather than vswap_si itself; resolve to the backing + * and test there. Returns false for zswap/zero/unbacked vswap entries + * as they don't have a backing bdev. + */ +static inline bool swap_entry_backend_has_flag(struct swap_info_struct *si, + swp_entry_t entry, + unsigned long flag) +{ + struct swap_info_struct *phys_si; + swp_entry_t phys; + bool has_flag; + + if (!swap_is_vswap(si)) + return data_race(si->flags & flag); + + phys = vswap_to_phys(entry); + if (!phys.val) + return false; + + phys_si = get_swap_device(phys); + if (IS_ERR_OR_NULL(phys_si)) + return false; + + has_flag = data_race(phys_si->flags & flag); + put_swap_device(phys_si); + return has_flag; +} + #endif /* _MM_VSWAP_H */ -- 2.53.1-Meta