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 6926E606A332 for ; Thu, 3 Dec 2026 18:15:57 +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=VVt8U45t Received: by kanga.kvack.org (Postfix) id 43B516B05DA; Thu, 3 Dec 2026 13:15:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3EBCA6B05DB; Thu, 3 Dec 2026 13:15:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DAC96B05DC; Thu, 3 Dec 2026 13:15:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 078F86B05DA for ; Thu, 3 Dec 2026 13:15:55 -0500 (EST) Received: from smtpin11.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9D67F407E7 for ; Thu, 3 Dec 2026 18:15:55 +0000 (UTC) X-FDA: 85503475470.11.7B2C202 Received: from mta0.migadu.com (out-99.mta0.migadu.com [91.218.175.99]) by imf08.hostedemail.com (Postfix) with ESMTP id 8EB20160011 for ; Thu, 3 Dec 2026 18:15:53 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=VVt8U45t; spf=pass (imf08.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.99 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796321753; b=XjUi+faCvjyQfxVs6o4wfiupWGTbcIj5Bnmf6w7nRqSEwW+eK2zqBZ15hk730bk1wOHoar m9rYq12lihkTTXvR5rkW88BSxW6ERmO2fVTT7QjXOqiLP/NxjKULPBmloOVf4wWga+RFsH fXal2idxJkuQ09iWOieci8moRmMczqA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321753; 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=Yrd/VgIxermmKjlgB3tVjwVdURkiCTags1hQGXkljJc=; b=ZPPHQiMJK5y0rIQF/ZKnfcXeGxTEC8wYMKRQzQMt0OAJAAmsfH5fqHRDCftTfip5WZLmnx o4qD1851L7a+piuQVb6yXsrTqDBw9UXdSokDlGD7JAiplsokoCITu4TxzU17ecqoV+071Y ICctP3XofxUatEq+zXpRoFndR+BVpeQ= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=VVt8U45t; spf=pass (imf08.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.99 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=gsmAy5Os76ECVTkrrKr9F+XvM39egozGDbQe3p4+7tE=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321752; v=1; x=1796926552; b=VVt8U45tLcol4lB9pWH/zfUijhVQo7LuHvxqCxpMyckUcbf+8sK74sHSiLPdsb/KCrmsfKq1 SSlWzjAm8b0zrU+a6hN0ON893ksqnpg8xhN5b9991uOr8YTLCo2PzQ9nroC3NuvdOfFiq+H/AIx 4erQ5qsiWZAAKc/KWs+SiC8Q= X-Envelope-To: linux-mm@kvack.org Received: by mta12.migadu.com with ESMTPS id ef16e1834f2e7413; Thu, 03 Dec 2026 18:15:52 +0000 X-Mizu-Trace-ID: ef16e1834f2e7413 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 23/32] mm, swap: uncharge an xswap backend where its record is cleared Date: Fri, 4 Dec 2026 02:13:10 +0800 Message-ID: <20261203181319.901844-24-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-Queue-Id: 8EB20160011 X-Stat-Signature: f3xq85o99m3dud71y7h1ypdz9gf41bqj X-Rspam-User: 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-Rspamd-Server: rspam06 X-HE-Tag: 1796321753-634073 X-HE-Meta: U2FsdGVkX18UjnGvE1QfbtfuB9DrdxDnc4rUdCMH3GRo8lU0LVxFghK8ubb2WHVDOhUHIDCeZCu4qP7QegrgT9zd6hBrur0nKygMOvmYF433rFApFNDRM5yzhDXQufDreUTy8/DvhnYB/H7wtTT1sZ/f6v5vqQrP4oT/Kp/rV+eEIzBOXtDWP5wclqXY8H5Qr02F5qJ0vpeheJ5HJo6zawzgSlALfTXHAfnDe9sHSHp5Q1bCQb6pZGp5CqtTrzXaS7HUiHvzLdR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oz2v/EvgU+jX3AesXhJrdY0wFUcilmssV3QHlHiynJo+LHUzHL9J0sHzRUNsQhZURue/6AOFnhot0jvIhYo9lboht/dFDRci8n 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 physical device drops the xswap entries that were written to it. The charge taken when the backend was allocated is released by xswap_release_slot_backend() when the xswap slot itself is freed, or by xswap_backend_free() when an allocation is rolled back. The record can be cleared before either runs: - the swapoff path clears it under the cluster lock, but uncharged outside it: a concurrent slot free can uncharge twice, or clear the id before the swapoff path reads it; - xswap_backend_free() clears it through __xswap_backend_unlink(), which does nothing when the record no longer matches, but it then uncharged unconditionally. Capture the owner id, clear the record, and uncharge only when the record still points at this entry, under the same cluster lock. Signed-off-by: Baoquan He --- mm/swapfile.c | 56 ++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index baebd30cf889..5de905970668 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -105,7 +105,6 @@ static int xswap_reclaim_backing(struct swap_info_struct *si, unsigned long offset, swp_entry_t xentry); static unsigned short xswap_entry_id(struct swap_cluster_info *ci, unsigned long off); -static void xswap_backend_uncharge(swp_entry_t entry, unsigned int nr); static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys, unsigned int order); @@ -3446,11 +3445,7 @@ static void xswap_release_slot_backend(struct swap_info_struct *si, if (psi) xswap_free_phys_slot(psi, entry, phys); - /* - * ci->lock is held here, so read the owner out of the swap table - * directly. xswap_backend_uncharge() would take the same lock - * again, and a spinlock is not recursive. - */ + /* ci->lock is held here, so read the owner out of the swap table directly. */ if (id) mem_cgroup_swap_uncharge(id, 1); } @@ -3683,17 +3678,23 @@ static int xswap_unuse_rmap(struct swap_info_struct *si, unsigned long offset) 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. + * Clear the record and uncharge under one lock. A concurrent slot + * free then cannot uncharge twice or clear the id before we read 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); + unsigned int slot = xoff % SWAPFILE_CLUSTER; + unsigned short id; + + if (xswap_slot_backend(xci, slot).val == phys.val) { + id = xswap_entry_id(xci, slot); + WRITE_ONCE(xci->xs_table[slot], 0); + if (id) + mem_cgroup_swap_uncharge(id, 1); + } swap_cluster_unlock(xci); } } @@ -4993,23 +4994,10 @@ static unsigned short xswap_entry_id(struct swap_cluster_info *ci, return __swap_cgroup_get(ci, off % SWAPFILE_CLUSTER); } -static void xswap_backend_uncharge(swp_entry_t entry, unsigned int nr) -{ - struct swap_info_struct *si = __swap_entry_to_info(entry); - struct swap_cluster_info *ci; - unsigned long off = swp_offset(entry); - unsigned short id = 0; - - ci = swap_cluster_lock(si, off); - if (ci) { - id = xswap_entry_id(ci, off); - swap_cluster_unlock(ci); - } - if (id) - mem_cgroup_swap_uncharge(id, nr); -} - -/* Drop the physical slot range and the reverse mapping, without any charging. */ +/* + * Drop the physical slot range and the reverse mapping, and uncharge what + * we clear. + */ static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys, unsigned int order) { @@ -5028,8 +5016,13 @@ static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys, unsigned int slot = (off + i) % SWAPFILE_CLUSTER; unsigned long *xs = ci->xs_table; - if (xs && xs[slot] == phys.val + i) + if (xs && xs[slot] == phys.val + i) { + unsigned short id = xswap_entry_id(ci, slot); + WRITE_ONCE(xs[slot], 0); + if (id) + mem_cgroup_swap_uncharge(id, 1); + } swap_cluster_unlock(ci); } @@ -5044,7 +5037,6 @@ static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys, void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order) { __xswap_backend_unlink(entry, phys, order); - xswap_backend_uncharge(entry, 1UL << order); } static int xswap_map_clusters(struct swap_info_struct *si, -- 2.54.0