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 33772606A32C for ; Thu, 3 Dec 2026 18:17:01 +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=W692loTJ Received: by kanga.kvack.org (Postfix) id A308D6B05E6; Thu, 3 Dec 2026 13:16:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 96D1E6B05E7; Thu, 3 Dec 2026 13:16:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8347A6B05E8; Thu, 3 Dec 2026 13:16:45 -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 5194A6B05E6 for ; Thu, 3 Dec 2026 13:16:45 -0500 (EST) Received: from smtpin24.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C8DBB1C416E for ; Thu, 3 Dec 2026 18:16:44 +0000 (UTC) X-FDA: 85503477528.24.EA9737D Received: from mta1.migadu.com (out-195.mta1.migadu.com [95.215.58.195]) by imf16.hostedemail.com (Postfix) with ESMTP id DCD6918000E for ; Thu, 3 Dec 2026 18:16:42 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=W692loTJ; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf16.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.195 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=1796321803; b=BJy/SL09KJlLDApkjf4933ty9BZqz0z8G+L99nm1m7/0Jm8R1GgF1Zpz/L4GXM1HHd2GYb cjjAP3v2QoibT5ODSbWXTP2BUzJiElxLpxOmXD4CF6vg3bezl7obCr4xgU50z/eO0HScvI CFHnfb5CDiz/ENddAGNzqcKpMcuiUOw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321803; 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=Mh9WkGvp5ByiDW6tGu8OYOgUcf27QRTn4c8gcsPFf/A=; b=tXk86eSsapvqinspYvnz99MT9yS6dmqmyCqNLcZIeJldAteSofFIi3HM01NUEarD/XMDWF NxbbD8HCWCDdppKiIpRTsQ904dycQ8y2/eaM6iLdDwkoLUwKb7zQOqZD7gx9/Sq+o/fkNH 93gwm8mxSeD3srswhRXnct0NfBpes+U= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=W692loTJ; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf16.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.195 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=kQSe3GF1WNcL6OhBfF3d1QRAT+oU+GZBOyul7KrCkPQ=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321801; v=1; x=1796926601; b=W692loTJ53g43csN4ZMs7vL5KkRBTUjrX/4rpqwvxIqhwl+iPAWGNJ5e1NAFMMmk540EvLlu 4wKSxgxYgwqyeNe6xiowewi76pXeZ4cnjbO2Tiuz8KiUEUnPDHgRVOQVAKtx3e1uxryC23PQHAx PAP7PTWn7A+UOIqiUjuAIuuc= X-Envelope-To: linux-mm@kvack.org Received: by mta11.migadu.com with ESMTPS id 90108d49dd9df0af; Thu, 03 Dec 2026 18:16:41 +0000 X-Mizu-Trace-ID: 90108d49dd9df0af 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 31/32] mm, swap: release a range of backend handles at once Date: Fri, 4 Dec 2026 02:13:18 +0800 Message-ID: <20261203181319.901844-32-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-Rspamd-Server: rspam05 X-Rspam-User: X-Rspamd-Queue-Id: DCD6918000E X-Stat-Signature: 4gzdq6oe7h994x4jb1jeswi1xbqqtef5 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: 1796321802-491230 X-HE-Meta: U2FsdGVkX18W10q4dxR0zZr0zNqU7yMmkUTZkOrG1a6d346gKxMp22Bc9x7KoP2Px3Atk2iLshvq6YmJ95hETliei2D/7brdWj4Eiyms/4jSVLN6vPZcGLgJWrEkbUQLzNulSU3Osypw1mYEjgmcxjsW4Xhln7eN6kcqgq4wulmF1kiJHn4t+WK9WrlQ7ce72w5do/4fOG7lY4qre5ITEChRTOUh/HIhsl81VE1Su9H18VIeMaa8PWSTxOjhsG9m/F7yHiIBpuVFZco55x8ZI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QA0DqO70mjEOCi/RaPdtUVN/Zir4m5e5qkhdGbAqJjBUPbATtmPUHAtW/3ybam2o5q5KEXSgO5p2qCPsEjhZvyMYQHTJDYENPOUmY8Xzog== 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 Freeing a range of swap slots calls swap_backend_erase() for every slot, which resolves the device and the cluster again each time: two percpu_ref_is_zero() calls and a division per slot. A slot range is a folio's slots or a cluster's free batch, so it never crosses a cluster. Resolve the cluster once and walk the slots. A tagged handle is only ever a zswap entry, so the walk releases those directly, which is why zswap_entry_free() is no longer static. The backend table is the only owner of a stored zswap entry, so the range erase is where it goes; collecting the handles first would only add an array or a callback to the free path. Release a tagged entry that is still in a cluster's table when the table goes as well, instead of only warning about it, so a broken invariant cannot leak it. Signed-off-by: Baoquan He --- include/linux/zswap.h | 4 ++++ mm/swap.h | 1 + mm/swapfile.c | 43 +++++++++++++++++++++++++++++++++++++++---- mm/zswap.c | 12 ++---------- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/include/linux/zswap.h b/include/linux/zswap.h index c7faad1d1b42..df9e0f29480e 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -6,6 +6,7 @@ #include struct lruvec; +struct zswap_entry; extern atomic_long_t zswap_stored_pages; @@ -25,6 +26,7 @@ struct zswap_lruvec_state { }; unsigned long zswap_total_pages(void); +void zswap_entry_free(struct zswap_entry *entry); bool zswap_store(struct folio *folio, bool may_enter_fs); int zswap_load(struct folio *folio); void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries); @@ -38,6 +40,8 @@ bool zswap_is_present(swp_entry_t entry, unsigned int nr); struct zswap_lruvec_state {}; +static inline void zswap_entry_free(struct zswap_entry *entry) {} + static inline bool zswap_store(struct folio *folio, bool may_enter_fs) { return false; diff --git a/mm/swap.h b/mm/swap.h index 9b2d8d556bd0..4f6e2c2de16f 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -229,6 +229,7 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp); void *swap_backend_get(swp_entry_t entry); void *swap_backend_set(swp_entry_t entry, void *handle); void *swap_backend_erase(swp_entry_t entry); +void swap_backend_erase_range(swp_entry_t entry, unsigned long nr); bool swap_backend_present(swp_entry_t entry, unsigned int nr); /* diff --git a/mm/swapfile.c b/mm/swapfile.c index 66a7682e29f0..3098a4086cdb 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -4884,10 +4884,40 @@ void *swap_backend_erase(swp_entry_t swp) return swap_backend_decode_zswap(val); } +/* + * Drop the handles of a range of slots. A range is a folio's slots or a + * cluster's free batch, so it never crosses a cluster: the caller holds + * either the cluster lock (slot free) or the folio lock (store rollback). + * + * Clamp and warn if the one-cluster invariant ever breaks; the next + * cluster is not protected here and the remaining slots are not erased. + */ +void swap_backend_erase_range(swp_entry_t swp, unsigned long nr) +{ + struct swap_cluster_info *ci = __swap_entry_to_cluster(swp); + unsigned int slot = swp_cluster_offset(swp); + unsigned long *tbl = READ_ONCE(ci->backend_table); + unsigned long n = min_t(unsigned long, nr, SWAPFILE_CLUSTER - slot); + + VM_WARN_ON_ONCE(nr > SWAPFILE_CLUSTER - slot); + if (!tbl) + return; + + for (unsigned int i = 0; i < n; i++) { + unsigned long val = READ_ONCE(tbl[slot + i]); + + if (!swap_backend_is_zswap(val)) + continue; + WRITE_ONCE(tbl[slot + i], 0); + zswap_entry_free(swap_backend_decode_zswap(val)); + } +} + /* * Drop the table of a cluster that is going away. An untagged value is an - * xswap physical backend that has to be given back; a tagged one would be a - * leak, the slot free path invalidates those first. Caller holds ci->lock. + * xswap physical backend that has to be given back; a tagged one should + * have been invalidated by the slot free path, but release it here too so a + * broken invariant cannot leak it. Caller holds ci->lock. */ static void swap_backend_table_free(struct swap_info_struct *si, struct swap_cluster_info *ci) @@ -4901,9 +4931,14 @@ static void swap_backend_table_free(struct swap_info_struct *si, for (slot = 0; slot < SWAPFILE_CLUSTER; slot++) { unsigned long val = READ_ONCE(tbl[slot]); - VM_WARN_ON_ONCE(swap_backend_is_zswap(val)); + if (swap_backend_is_zswap(val)) { + VM_WARN_ON_ONCE(1); + WRITE_ONCE(tbl[slot], 0); + zswap_entry_free(swap_backend_decode_zswap(val)); + continue; + } #ifdef CONFIG_XSWAP - if (val && !swap_backend_is_zswap(val)) + if (val) xswap_release_slot_backend(si, ci, slot); #else VM_WARN_ON_ONCE(val); diff --git a/mm/zswap.c b/mm/zswap.c index 1903367a9576..9df2eaf4a21c 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -766,7 +766,7 @@ static void zswap_entry_cache_free(struct zswap_entry *entry) * Carries out the common pattern of freeing an entry's zsmalloc allocation, * freeing the entry itself, and decrementing the number of stored pages. */ -static void zswap_entry_free(struct zswap_entry *entry) +void zswap_entry_free(struct zswap_entry *entry) { struct zswap_pool *pool = zswap_entry_pool(entry); @@ -1849,8 +1849,6 @@ int zswap_load(struct folio *folio) void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries) { - unsigned long i; - if (zswap_never_enabled()) return; @@ -1859,13 +1857,7 @@ void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries) * no longer allocatable, and the store rollback holds the folio * lock, so only one caller can see a tagged handle. */ - for (i = 0; i < nr_entries; i++) { - struct zswap_entry *entry; - - entry = swap_backend_erase(swp_entry(type, offset + i)); - if (entry) - zswap_entry_free(entry); - } + swap_backend_erase_range(swp_entry(type, offset), nr_entries); } /********************************* -- 2.54.0