From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335226; b=VYgTRtTx+FyhPBLuIe6w22bYVjwV+D4vF9jh/HCmkA4MJD4dGRULao7168kWrBOchuCh +dquM3f4PiL9YSCtxDaRVHVTcvLEO2IbQUtVnkDZmht1Xwx8zJDc5//jjHHIkt4qzgI8l FzNQ+Fs0LlmKiiMuX/wYQSnVNmSdBCcdxAPQN5ee8jJknlI7AnF0Zdv02HCvfqbFNYZeH wUZfJjQddViOwqwyo9ZJA3jCtxWj8RoohaJL2fGEx/IMS8qXg3UsL7Z9XuZ4FtX89938X MxSVz/DLkUz0hbGdYRr8omnE5LNlg+NduXx3KAlXjDpNxKU5qKJGZrtKQl+0ty8xMBQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335226; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=tgEFLLKda8KoAA4WLRXHKZ3ICOkv1pZahiwwTAQfRO4=; b=vEqx2jURdRRzeaqqFl+RDycu34ZVs4T29L9A+RVnHfV3WJVBm6V7B18Z/Wj2jiZM0by8 fR9AiHGZU5M47CRoDRwUlPCLmeaqDRpRvoSWGn6/FT4+0fxtqjxcHAxxmPJub+nx3wNKu zpO1UlEWjoSvMMQX0JOqArndWaLNu+kXrx8VVrykoTEVFVL0akbWtIcJMz4Z3sSMd+mPl h0mA0AZE6m2vwjPl6jsjieVL6YPkCLe+j6dFiwq46JWlbHG29hxYrGjTCura/Fw4MPMY5 842rF+XTJUyY6NGQSn9aeDoUqYhx91McZ1wKjiFLjDkp9hymW8YEDVgkv2s6VnKzsww== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=w5Bd5HwK; arc=none smtp.remote-ip=209.85.128.169 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2848606A695 for ; Thu, 3 Dec 2026 22:00:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2848606A695 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.a=rsa-sha256 header.s=20260707 header.b=w5Bd5HwK Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-8b0ca1ebc8bso2552487b3.2 for ; Thu, 03 Dec 2026 14:00:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335225; x=1796940025; darn=vger.kernel.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=tgEFLLKda8KoAA4WLRXHKZ3ICOkv1pZahiwwTAQfRO4=; b=w5Bd5HwKWmkMZ8uR8O5/QsJU9Rm8keFO1K3I+b4mdwNHuvIXoYnUThnQG9VSqBTfkg hJCgMUh0o4yW9v9wuZOcvQV6RAPqkvlztrPD10DRdgESrnc+TttZRmb0FIOVKC/25Gf8 WU7aU5JkpJSVguYm6/AqXhJjFSDw245BEv5UBjfSbbS5UXtk2RtrhkGYlh6GFEq3Jnat pvzuUQbXF8AlQhWUOLua80XtjOx0h/CSVmgI/n/iSKAbNnEXp1tRDbDP96yI+6MZbmmV M5/lswI7ZUSTK+8nX9YbBo23KG/p8AhDtXhUA0yuez14/Xu9FsnhuV2Q4YUF+rPqZeCD ffOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335225; x=1796940025; 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=tgEFLLKda8KoAA4WLRXHKZ3ICOkv1pZahiwwTAQfRO4=; b=cTfEalfsBycpblQUyEpTnVa8BFHLvDJmw9uns/inYPJ58HHihhhc/hJqKuAdOqx3ou 4t6kqM48KSNF4OqExCIz/CrIUSHq1rg64NUajqLe+QnGAepiUEQrYtK+QNnlQ/rFbLZp 2ztSPwB8jW7OYSatj/qrgLUWVYsNqlISYa5k7Bn/BYB6XgAhsQv8kwRWPfPyrgfLOqb9 d/4CcK4LuF0VBjmQPsGwWsZgevYfs5oHfoVeu0uAZTjKbhg9HkXhyljWYkRi0JACyiL4 1cKMZB9NUw7YSmeDugCEEGnUNriRdbF4SsONWVIsT/6IxDNKduEUTOi+qgqhYHbiJU8T XxlQ== X-Forwarded-Encrypted: i=1; AL+hErHG8hDx1OwSc3KIqBfc+mLA9vXEwBDyp+KoE/oqOPSIjDw3r7cGKGujPF1nJIQeIdi78pS9uXvA@vger.kernel.org X-Gm-Message-State: APuU4Z9C71loUneQK72Rz6ZLrPyRTs27H1SfOcqGMYV8lV8LsF6w/kUP +cU9jVr5Sn15IM7sfpuCileppU1R4lqP5BAZ/AXzAVij+YyJI0E/FM9O X-Gm-Gg: AZFdTZeEa93FWVh2dbsjc42BHA97SbCzNMvAOQ8eah5WpTdSNHSzN8/Jll9AxGFYvRR INpXQJ2KeHh+HY19t/xFUZazp4NFg2GioKDYNPX1EK1aKoarIlImj8YX6/UNpQSX+7mLGdduk9O VRqtmOxv1OKVrVHXxrVxNb7oZV87nOqWxEDL8tNy7X0NGzB0SwCbJ4DoA9TZ0xXRQLGNHHmuJWF lcIEWyQ4O98X1VZ2ew+xpeuZrGh5ZGWJlIVm0/bio+sbB8Ke3JcsO98ydhutMfRwi0tEgm60d/A M6bpHxYbQQfiKUGC/5KNF5gXl8gr7FtX2XJ1PN1eibftDqgTRW3bLFLM292VhgMHQhed3UlWcoO RZis/uSiMUJ/2FN12bXYcKdmNOzR/DH96Ucq9ee/hVEJYppf+ImxQgRrNmBW2UnGy2MKF0nVc+C YddF07JPxAi+6oE0exh2MIr1VPRqUWaMW9EdixUCx2lRytufQ6kyo4aJp6tbE0rpvz4SNrssEc5 GexeA43d7Zr0facNoL0lGdwOQi4I2JCx8ujTWKHKjD337s4QjgXJvG76qgT2Nl4nj3K31ya9ldS alsrjR/dcmVCzC6qs5l5cKQOTizZSybzmwf6OWXs4SRQYJwkN/rasnsd28+3w6GqS4xEKJFfvlv fniE5FcYTknH9TnjuMvkdiMkFN9Wt0TLeu5Io6A== X-Received: by 2002:a05:690c:4cb5:b0:8b4:ce27:9938 with SMTP id 00721157ae682-8b659db8f50mr1680567b3.1.1796335224678; Thu, 03 Dec 2026 14:00:24 -0800 (PST) Received: from localhost ([2a03:2880:10ff:48::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660df8d33sm971797b3.39.2026.12.03.14.00.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:24 -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 07/20] mm/swap: prepare physical swapoff for virtual backing Date: Thu, 3 Dec 2026 13:59:57 -0800 Message-ID: <20261203220010.1302207-8-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-Mailing-List: cgroups@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Physical swapoff matches the swap type in PTEs and shmem entries. That match misses virtual entries, whose type stays unchanged when their physical backing changes. Match the physical device through the vtable and resolve physical reverse maps to their virtual entry during the cache walk. Recheck backing under the folio lock before installing data or freeing slots. Use order-0 allocations for fresh virtual reads during unuse. Read shmem entries and their order together under the XArray lock so collapse cannot give the scan mismatched values. Prepare these consumers before the physical backend publishes reverse maps, ensuring swapoff can find the entries backed by the device being removed. Signed-off-by: Nhat Pham --- mm/shmem.c | 67 ++++++++++++++++++++++++++++++++++++-------- mm/swap.h | 6 ++++ mm/swap_table.h | 39 +++++++++++++++++++++++--- mm/swapfile.c | 74 +++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 162 insertions(+), 24 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 344f7edf5e15..5d6807518474 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -87,6 +87,7 @@ static struct vfsmount *shm_mnt __ro_after_init; #include #include "internal.h" +#include "vswap.h" #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) @@ -161,7 +162,8 @@ static unsigned long shmem_default_max_inodes(void) static int shmem_swapin_folio(struct inode *inode, pgoff_t index, struct folio **foliop, enum sgp_type sgp, gfp_t gfp, - struct vm_fault *vmf, vm_fault_t *fault_type); + struct vm_fault *vmf, vm_fault_t *fault_type, + int swapoff_type); static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) { @@ -1347,7 +1349,8 @@ static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index) * Truncation can look up a large swap entry at or beyond i_size. * Swap it in directly to bypass the size check in shmem_get_folio(). */ - if (shmem_swapin_folio(inode, index, &folio, SGP_READ, gfp, NULL, NULL) == -EEXIST) + if (shmem_swapin_folio(inode, index, &folio, SGP_READ, gfp, + NULL, NULL, -1) == -EEXIST) goto retry; return folio; } @@ -1710,6 +1713,24 @@ static void shmem_evict_inode(struct inode *inode) #endif } +static bool shmem_find_swapoff_entry(swp_entry_t entry, pgoff_t *index, + unsigned int order, unsigned int type) +{ + unsigned long nr = 1UL << order; + unsigned long off = *index & (nr - 1); + + if (!softleaf_is_swap(entry)) + return false; + entry.val += off; + for (; off < nr; off++, entry.val++) { + if (swap_entry_is_backed_by(entry, type)) { + *index = round_down(*index, nr) + off; + return true; + } + } + return false; +} + static unsigned int shmem_find_swap_entries(struct address_space *mapping, pgoff_t start, struct folio_batch *fbatch, pgoff_t *indices, unsigned int type) @@ -1717,6 +1738,8 @@ static unsigned int shmem_find_swap_entries(struct address_space *mapping, XA_STATE(xas, &mapping->i_pages, start); struct folio *folio; swp_entry_t entry; + pgoff_t index; + unsigned int order; rcu_read_lock(); xas_for_each(&xas, folio, ULONG_MAX) { @@ -1726,15 +1749,27 @@ static unsigned int shmem_find_swap_entries(struct address_space *mapping, if (!xa_is_value(folio)) continue; + /* A concurrent collapse can change both the value and its order. */ + xas_lock_irq(&xas); + xas_set(&xas, xas.xa_index); + folio = xas_load(&xas); + if (!xa_is_value(folio)) { + xas_unlock_irq(&xas); + continue; + } entry = radix_to_swp_entry(folio); + index = xas.xa_index; + order = xas_get_order(&xas); + xas_unlock_irq(&xas); /* * swapin error entries can be found in the mapping. But they're * deliberately ignored here as we've done everything we can do. */ - if (swp_type(entry) != type) + if (!shmem_find_swapoff_entry(entry, &index, + order, type)) continue; - indices[folio_batch_count(fbatch)] = xas.xa_index; + indices[folio_batch_count(fbatch)] = index; if (!folio_batch_add(fbatch, folio)) break; @@ -1753,7 +1788,7 @@ static unsigned int shmem_find_swap_entries(struct address_space *mapping, * of pages swapped in, or the error in case of failure. */ static int shmem_unuse_swap_entries(struct inode *inode, - struct folio_batch *fbatch, pgoff_t *indices) + struct folio_batch *fbatch, pgoff_t *indices, unsigned int type) { int i = 0; int ret = 0; @@ -1764,7 +1799,7 @@ static int shmem_unuse_swap_entries(struct inode *inode, struct folio *folio = fbatch->folios[i]; error = shmem_swapin_folio(inode, indices[i], &folio, SGP_CACHE, - mapping_gfp_mask(mapping), NULL, NULL); + mapping_gfp_mask(mapping), NULL, NULL, type); if (error == 0) { folio_unlock(folio); folio_put(folio); @@ -1796,7 +1831,7 @@ static int shmem_unuse_inode(struct inode *inode, unsigned int type) break; } - ret = shmem_unuse_swap_entries(inode, &fbatch, indices); + ret = shmem_unuse_swap_entries(inode, &fbatch, indices, type); if (ret < 0) break; @@ -2527,18 +2562,19 @@ static int shmem_split_large_entry(struct inode *inode, pgoff_t index, static int shmem_swapin_folio(struct inode *inode, pgoff_t index, struct folio **foliop, enum sgp_type sgp, gfp_t gfp, struct vm_fault *vmf, - vm_fault_t *fault_type) + vm_fault_t *fault_type, int swapoff_type) { struct address_space *mapping = inode->i_mapping; struct vm_area_struct *vma = vmf ? vmf->vma : NULL; struct mm_struct *fault_mm = vmf ? vmf->vma->vm_mm : NULL; struct shmem_inode_info *info = SHMEM_I(inode); - swp_entry_t swap; + swp_entry_t swap, target_swap; softleaf_t index_entry; struct swap_info_struct *si; struct folio *folio = NULL; int error, nr_pages, order; pgoff_t offset; + bool vswap_unuse; VM_BUG_ON(!*foliop || !xa_is_value(*foliop)); index_entry = radix_to_swp_entry(*foliop); @@ -2566,14 +2602,16 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, offset = index - round_down(index, 1 << order); swap = swp_entry(swp_type(swap), swp_offset(swap) + offset); } + target_swap = swap; + vswap_unuse = swapoff_type >= 0 && swap_is_vswap(si); /* Look it up and read it in.. */ folio = swap_cache_get_folio(swap); if (!folio) { - if (data_race(si->flags & SWP_SYNCHRONOUS_IO)) { + if (vswap_unuse || data_race(si->flags & SWP_SYNCHRONOUS_IO)) { /* Direct swapin skipping swap cache & readahead */ folio = shmem_swap_alloc_folio(inode, vmf, index, - swap, order, gfp); + swap, vswap_unuse ? 0 : order, gfp); } else { /* Cached swapin only supports order 0 folio */ folio = shmem_swapin_cluster(swap, gfp, info, index); @@ -2648,6 +2686,11 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, goto failed; } folio_wait_writeback(folio); + if (swapoff_type >= 0 && + !swap_entry_is_backed_by(target_swap, swapoff_type)) { + error = -EEXIST; + goto unlock; + } /* * Some architectures may have to restore extra metadata to the @@ -2748,7 +2791,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, if (xa_is_value(folio)) { error = shmem_swapin_folio(inode, index, &folio, - sgp, gfp, vmf, fault_type); + sgp, gfp, vmf, fault_type, -1); if (error == -EEXIST) goto repeat; diff --git a/mm/swap.h b/mm/swap.h index 7b9a65232d19..018250b814bb 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -309,6 +309,7 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp); int folio_alloc_swap(struct folio *folio); int folio_dup_swap(struct folio *folio, struct page *page); void folio_put_swap(struct folio *folio, struct page *page); +bool swap_entry_is_backed_by(swp_entry_t entry, unsigned int type); /* For internal use */ extern void __swap_cluster_free_entries(struct swap_info_struct *si, @@ -460,6 +461,11 @@ static inline int folio_alloc_swap(struct folio *folio) return -EINVAL; } +static inline bool swap_entry_is_backed_by(swp_entry_t entry, unsigned int type) +{ + return false; +} + static inline int folio_dup_swap(struct folio *folio, struct page *page) { return -EINVAL; diff --git a/mm/swap_table.h b/mm/swap_table.h index 9c9e3cd3b37e..36b17f03bd9b 100644 --- a/mm/swap_table.h +++ b/mm/swap_table.h @@ -4,6 +4,7 @@ #include #include +#include #include "swap.h" extern struct swap_info_struct *vswap_si; @@ -30,7 +31,7 @@ struct swap_memcg_table { * NULL: |---------------- 0 ---------------| - Free slot * Shadow: |SWAP_COUNT|Z|---- SHADOW_VAL ---|1| - Swapped out slot * PFN: |SWAP_COUNT|Z|------ PFN -------|10| - Cached slot - * Pointer: |----------- Pointer ----------|100| - (Unused) + * Vswap: |-------- vswap offset --------|100| - vswap rmap * Bad: |------------- 1 -------------|1000| - Bad slot * HIB: | 0 |------- 1 -------|10000| - Hibernation slot * @@ -52,9 +53,8 @@ struct swap_memcg_table { * - PFN: Swap slot is in use, and cached. Memcg info is recorded on the page * struct. * - * - Pointer: Unused yet. `0b100` is reserved for potential pointer usage - * because only the lower three bits can be used as a marker for 8 bytes - * aligned pointers. + * - Vswap: Reverse map from a physical slot to the vswap entry that owns + * it. See the layout below. * * - Bad: Swap slot is reserved, protects swap header or holes on swap devices. * @@ -407,4 +407,35 @@ static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info *ci, } #endif +/* + * Vswap-tagged swap table entry: rmap for vswap-backing physical slots. + * + * On physical clusters, a Vswap-tagged entry stores the offset of the + * vswap entry that owns this physical slot (the reverse map). Only the + * offset is stored; the swap type is implicit (always vswap_si->type, + * since there is exactly one vswap device). + * + * Vswap: |---- vswap offset ----|100| + */ +#define SWP_TB_VSWAP_MARK_BITS 3 +#define SWP_TB_VSWAP_MARK 0b100UL +#define SWP_TB_VSWAP_MARK_MASK ((1UL << SWP_TB_VSWAP_MARK_BITS) - 1) +#define SWP_RMAP_ENTRY_MASK (~SWP_TB_VSWAP_MARK_MASK) + +static inline bool swp_tb_is_vswap(unsigned long swp_tb) +{ + return (swp_tb & SWP_TB_VSWAP_MARK_MASK) == SWP_TB_VSWAP_MARK; +} + +#ifdef CONFIG_SWAP +static inline swp_entry_t swp_tb_vswap_to_swp_entry(unsigned long swp_tb) +{ + unsigned long offset; + + VM_WARN_ON(!swp_tb_is_vswap(swp_tb)); + offset = (swp_tb & SWP_RMAP_ENTRY_MASK) >> SWP_TB_VSWAP_MARK_BITS; + return swp_entry(vswap_si->type, offset); +} +#endif + #endif diff --git a/mm/swapfile.c b/mm/swapfile.c index d9e8c9365c44..6933e069bd2e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2807,13 +2807,36 @@ static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte) return pte_same(pte_swp_clear_flags(pte), swp_pte); } +bool swap_entry_is_backed_by(swp_entry_t entry, unsigned int type) +{ + struct swap_info_struct *si; + swp_entry_t phys; + bool match = false; + + if (!softleaf_is_swap(entry)) + return false; + /* Direct entries retain their physical slots while zswapped. */ + if (swp_type(entry) == type) + return true; + si = get_swap_device(entry); + if (IS_ERR_OR_NULL(si)) + return false; + if (swap_is_vswap(si)) { + phys = vswap_to_phys(entry); + match = phys.val && swp_type(phys) == type; + } + put_swap_device(si); + return match; +} + /* * No need to decide whether this PTE shares the swap entry with others, * just let do_wp_page work it out if a write is requested later - to * force COW, vm_page_prot omits write permission from any private vma. */ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, - unsigned long addr, swp_entry_t entry, struct folio *folio) + unsigned long addr, swp_entry_t entry, struct folio *folio, + unsigned int type) { struct page *page; struct folio *swapcache; @@ -2826,14 +2849,16 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, * If the folio is removed from swap cache by others, continue to * unuse other PTEs. try_to_unuse may try again if we missed this one. */ - if (!folio_matches_swap_entry(folio, entry)) + if (!folio_matches_swap_entry(folio, entry) || + !swap_entry_is_backed_by(entry, type)) return 0; /* * A large folio that a fault didn't map can't be mapped one PTE at a * time here, so drop it and swap in small folios instead. */ - if (unlikely(!swap_cache_folio_mappable(folio, 1))) { + if (unlikely(!is_vswap_entry(entry) && + !swap_cache_folio_mappable(folio, 1))) { swap_cache_del_folio(folio); return 0; } @@ -2875,6 +2900,18 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, goto setpte; } + if (is_vswap_entry(entry) && !folio_test_anon(folio) && + folio_test_large(folio)) { + if (folio_test_dirty(folio)) { + ret = -EBUSY; + goto out; + } + /* A fresh large folio has no partial anonymous rmap yet. */ + swap_cache_del_folio(folio); + ret = 0; + goto out; + } + /* * Some architectures may have to restore extra metadata to the page * when reading from swap. This metadata may be indexed by swap entry @@ -2959,7 +2996,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, if (!softleaf_is_swap(entry)) continue; - if (swp_type(entry) != type) + if (!swap_entry_is_backed_by(entry, type)) continue; pte_unmap(pte); @@ -2974,8 +3011,15 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, .pmd = pmd, }; - folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, - &vmf); + if (is_vswap_entry(entry)) { + folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE, + BIT(0), &vmf, NULL, 0); + if (IS_ERR(folio)) + folio = NULL; + } else { + folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, + &vmf); + } } if (!folio) { swp_tb = swap_table_lookup(entry); @@ -2986,7 +3030,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, folio_lock(folio); folio_wait_writeback(folio); - ret = unuse_pte(vma, pmd, addr, entry, folio); + ret = unuse_pte(vma, pmd, addr, entry, folio, type); if (ret < 0) { folio_unlock(folio); folio_put(folio); @@ -3167,7 +3211,8 @@ static int try_to_unuse(unsigned int type) int retval = 0; struct swap_info_struct *si = swap_info[type]; struct folio *folio; - swp_entry_t entry; + swp_entry_t entry, phys; + unsigned long swp_tb; unsigned int i; if (!swap_usage_in_pages(si)) @@ -3216,6 +3261,10 @@ static int try_to_unuse(unsigned int type) (i = find_next_to_unuse(si, i)) != 0) { entry = swp_entry(type, i); + + swp_tb = swap_table_lookup(entry); + if (swp_tb_is_vswap(swp_tb)) + entry = swp_tb_vswap_to_swp_entry(swp_tb); folio = swap_cache_get_folio(entry); if (!folio) continue; @@ -3228,7 +3277,16 @@ static int try_to_unuse(unsigned int type) */ folio_lock(folio); folio_wait_writeback(folio); + if (is_vswap_entry(entry)) { + if (!folio_matches_swap_entry(folio, entry)) + goto unlock_cache; + phys = vswap_to_phys(entry); + if (!phys.val || swp_type(phys) != type || + swp_offset(phys) != i) + goto unlock_cache; + } folio_free_swap(folio); +unlock_cache: folio_unlock(folio); folio_put(folio); } -- 2.53.1-Meta