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 15099606A322 for ; Thu, 3 Dec 2026 18:14:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=w8KNsbF8 Received: by kanga.kvack.org (Postfix) id 34CE46B05BE; Thu, 3 Dec 2026 13:14:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2FD646B05BF; Thu, 3 Dec 2026 13:14:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1ECB66B05C0; Thu, 3 Dec 2026 13:14:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id ED3706B05BE for ; Thu, 3 Dec 2026 13:14:04 -0500 (EST) Received: from smtpin20.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 8A9AA1C3CF5 for ; Thu, 3 Dec 2026 18:14:04 +0000 (UTC) X-FDA: 85503470808.20.57DC499 Received: from mta0.migadu.com (out-2.mta0.migadu.com [91.218.175.2]) by imf02.hostedemail.com (Postfix) with ESMTP id F1D2B80004 for ; Thu, 3 Dec 2026 18:14:01 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=w8KNsbF8; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.2 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796321642; b=oXF+EFd2Slq13MM/ifvRTI9Z1g9IKYl38z6HViF43gmX04JQdc5xIg1UzPBV+rxLbXMTIK DlB7M51sdU2+eJtCDUe+KqUF8EUVDLh8MIIBJFgDXLeTu0Xwa0dsT2NNeShChErNmYtuWw dm07PezuauWrEdbw+xZxuSC8Jewv2wo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321642; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NOLYTFpkut6aI7b5NbQB8HpJeImurlnLB4Aq2v/Um0U=; b=zyl9L2LbUi/zHPStBq5aTuWBbLFnWmGu41O/MXR/J+9x37qkP8Xm5K4iHdjYpf6Jvr8jOZ NV0X2Q1uhusDXb5N0PzI951wTmu35z4YxncYiuBN81dQIAyfh7ju6DtOWWubpwZaFFRSpu p8yC53Y4ioH14ocnXqyjSbMai8Hpbx8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=w8KNsbF8; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.2 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=hjbxFkBQQmpbAVyqrG5sYa7c9hs773KGrBSk2lOYbDg=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321640; v=1; x=1796926440; b=w8KNsbF8rEwX1FoVqzFXeEKp/Y92hGJ/sdn3wTScPqZ9CORipKHzd9zEFG+JEHBHy1cnZPiE QSgr7TUznlQtO1p6FO37RJDtN8SxZosY/nEFF72K+yBXAfc69jzZa8o7ckM6nQSNsqAd+k+MyXu R2SZ2VIKHULulYFmYuM3HtaU= X-Envelope-To: linux-mm@kvack.org Received: by mta11.migadu.com with ESMTPS id bc74169da275fc73; Thu, 03 Dec 2026 18:14:00 +0000 X-Mizu-Trace-ID: bc74169da275fc73 X-Migadu-Flow: FLOW_OUT From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org, kasong@tencent.com, hannes@cmpxchg.org, corbet@lwn.net, yosry@kernel.org, nphamcs@gmail.com, muchun.song@linux.dev, baohua@kernel.org, youngjun.park@lge.com, hughd@google.com, baolin.wang@linux.alibaba.com, baoquan.he@linux.dev, Baoquan He Subject: [PATCH v2 05/32] mm, swap: support swapoff of an xswap physical backend Date: Fri, 4 Dec 2026 02:12:52 +0800 Message-ID: <20261203181319.901844-6-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev> References: <20261203181319.901844-1-baoquan.he@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Stat-Signature: 9upbajay3qyysrx9mc88i11asee9gb6j X-Rspamd-Queue-Id: F1D2B80004 X-Rspam-User: X-Rspamd-Server: rspam10 X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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: 1796321641-830123 X-HE-Meta: U2FsdGVkX18175YU07TXKjBnWbN2xAkpxttLJIKVImgmb2nFESU0WmFsvOt5w4M45CTgGJhBrcXclRrf6KOq9dk7dgGoH0kPsP9J7LZT4/ryQVJalrwir9ej4Nd4BMj4wZXUFukt4T7S5tmPXmd/JhzlbcH3BS1Wh9RnPcoGPI8Yl0wO2xovEPqSmN0y0T/TmffCkoEEIKGMFr9Y2SKDRrR0QOvBMUqtJmB3u8f+IIMamISlr053+a/soukLrVEXA+cU4op0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lOqk2lXop9CG6rqLaZVGfHASMZpk9n8Wy2gKcFNprDAiwJlYkTiHvBp+2/KzEjSABV7m1tcT9ZYDJS28SZzSK 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 From: Baoquan He swapoff of a real swap device iterates its slots through try_to_unuse() and reads each one back into memory. A slot that was written back from an xswap entry has no folio in the swap cache, so the loop skips it and the slot is freed without ever reading the data. While the xswap entry will keep pointing at a slot that no longer holds anything. Now change try_to_unuse() to analyze such a slot through its reverse mapping: bring the folio back, mark it dirty so that reclaim stores it into zswap again, and only then drop the backend. Signed-off-by: Baoquan He --- mm/swapfile.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 149 insertions(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 9eb4284e9bc0..a9d7ba3d2692 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -3361,6 +3362,140 @@ static void xswap_release_slot_backend(struct swap_info_struct *si, xswap_free_phys_slot(psi, entry, phys); } +/* + * A written-back slot has no folio of its own here, so the unuse loop would + * skip it. Resolve it through the reverse mapping and free both sides. + */ +static int xswap_unuse_rmap(struct swap_info_struct *si, unsigned long offset) +{ + struct swap_cluster_info *pci, *xci; + struct swap_info_struct *xsi; + struct swap_io_ctx ctx = {}; + struct mempolicy *mpol; + struct folio *folio; + unsigned long mapped, swp_tb, xoff; + swp_entry_t xentry, phys; + bool drop = false; + int ret = 0; + + phys = swp_entry(si->type, offset); + + pci = swap_cluster_lock(si, offset); + if (!pci) + return 0; + /* + * The cluster can have emptied since find_next_to_unuse() read its + * count without the lock, and freeing a cluster clears its table. + */ + if (!cluster_table_is_alloced(pci)) { + swap_cluster_unlock(pci); + return 0; + } + swp_tb = __swap_table_get(pci, offset % SWAPFILE_CLUSTER); + if (!swp_tb_is_pointer(swp_tb)) { + swap_cluster_unlock(pci); + return 0; + } + xentry = xswap_rmap_to_entry(swp_tb); + swap_cluster_unlock(pci); + + xoff = swp_offset(xentry); + + /* + * Pin the owner before reading its flags or cluster_info: + * free_swap_cluster_info() only unmaps that after killing si->users. + * No owner left means the slot is dropped below. + */ + xsi = swap_type_to_info(swp_type(xentry)); + if (!xsi || !get_swap_device_info(xsi)) + goto out_free; + /* The record can outlive the device, or name a reused type slot. */ + if (!(xsi->flags & SWP_XSWAP)) { + put_swap_device(xsi); + goto out_free; + } + + /* + * An owner's slot is inside its mapped range; one past it is gone. + * Pairs with the smp_store_release() in xswap_map_clusters(). + */ + mapped = smp_load_acquire(&xsi->nr_clusters_mapped); + if (xoff >= mapped * SWAPFILE_CLUSTER) { + /* Owner gone, so the slot is an orphan and has to go. */ + drop = true; + goto out_put; + } + + /* + * Bring the data back before the slot goes. A dirty folio is what + * reclaim stores into zswap next time. + */ + folio = swap_cache_get_folio(xentry); + if (!folio) { + mpol = get_task_policy(current); + folio = __swap_cache_alloc_folio(xentry, GFP_HIGHUSER_MOVABLE, + BIT(0), NULL, mpol, + NO_INTERLEAVE_INDEX); + if (IS_ERR(folio)) { + ret = PTR_ERR(folio) == -ENOMEM ? -ENOMEM : -EAGAIN; + goto out_put; + } + /* A folio off the LRU would be lost if a fault took it. */ + folio_add_lru(folio); + swap_read_folio(&ctx, folio); + swap_read_submit(&ctx); + folio_lock(folio); + } else { + folio_lock(folio); + } + + if (folio_matches_swap_entry(folio, xentry)) { + folio_wait_writeback(folio); + if (unlikely(!folio_test_uptodate(folio))) { + /* The read itself failed; a retry cannot help. */ + swap_cache_del_folio(folio); + ret = -EIO; + } else { + folio_mark_dirty(folio); + drop = true; + } + } else { + /* Taken from under us, so nothing was read back either. */ + ret = -EAGAIN; + } + folio_unlock(folio); + folio_put(folio); + + /* + * The slot is going away, so drop the record that names it. The + * mapped range cannot shrink while the device is pinned, so @xoff + * still has a cluster here whenever @drop is set. + */ + if (drop) { + xci = swap_cluster_lock(xsi, xoff); + if (xci) { + if (xswap_slot_backend(xci, xoff % SWAPFILE_CLUSTER).val == + phys.val) + WRITE_ONCE(xci->xs_table[xoff % SWAPFILE_CLUSTER], + 0); + swap_cluster_unlock(xci); + } + } +out_put: + put_swap_device(xsi); + if (!drop) + return ret; +out_free: + /* Leaving the mapping in place would make the unuse loop spin. */ + xswap_free_phys_slot(si, xentry, phys); + return 0; +} +#else +static inline int xswap_unuse_rmap(struct swap_info_struct *si, + unsigned long offset) +{ + return 0; +} #endif /* CONFIG_XSWAP */ static int try_to_unuse(unsigned int type) { @@ -3420,8 +3555,21 @@ static int try_to_unuse(unsigned int type) entry = swp_entry(type, i); folio = swap_cache_get_folio(entry); - if (!folio) + if (!folio) { + /* + * Only a physical backend holds pointer entries. An + * xswap device's slots are shadows, and its + * cluster_info is what find_next_to_unuse() reads + * without the lock. + */ + if (!(si->flags & SWP_XSWAP)) { + retval = xswap_unuse_rmap(si, i); + /* A lost race is retried on the next pass. */ + if (retval && retval != -EAGAIN) + return retval; + } continue; + } /* * It is conceivable that a racing task removed this folio from -- 2.54.0