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 18F45606A32C for ; Thu, 3 Dec 2026 18:15:49 +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=dA7oh8qK Received: by kanga.kvack.org (Postfix) id 302C96B05D6; Thu, 3 Dec 2026 13:15:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 265A76B05D7; Thu, 3 Dec 2026 13:15:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1566C6B05D8; Thu, 3 Dec 2026 13:15:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D268B6B05D6 for ; Thu, 3 Dec 2026 13:15:42 -0500 (EST) Received: from smtpin11.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 628A0C0162 for ; Thu, 3 Dec 2026 18:15:42 +0000 (UTC) X-FDA: 85503474924.11.9968AF8 Received: from mta0.migadu.com (out-86.mta0.migadu.com [91.218.175.86]) by imf19.hostedemail.com (Postfix) with ESMTP id 56D6D1A000C for ; Thu, 3 Dec 2026 18:15:40 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dA7oh8qK; spf=pass (imf19.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.86 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=1796321740; b=KoOCiHk0QuPDI85cxWP+EYfLqET9OOGLf48jRd+WMiHetf7kBGnRiy+ZzXYX6DVqpIIMAi 1nvOf6fHpbgQepy4tC9YqLKs+K+Xtsc5En+VGSntAnPnXT4CttUyNPc48RsZJlxquNuA/n 4kfiz5zsDvi8RLxMXYRNBBDf8t2kEXI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321740; 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=hKtXbFWYjDLDDyBePHfIdc3rElEQck5FTY9pLmiIK1U=; b=NUFmNXV9EesqjcgrDH12kDg8qeQ8fER7nuADZV0oXjomXfQcsW28Vjtjk1BcKEFG2QHoak p9/aPnPu+eLjBbJPdzYWcvgUxOswW/zrqQSWY6uHExTNJAPAFHzWh+8lNsUVF/8XIp1rlC ziKE95ognMmEexYAvL/GS8kFsFIMFns= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dA7oh8qK; spf=pass (imf19.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.86 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=c5J3q+GxICf85hz9Jqs/tGJ3EfL+RwyDhMaRYqzJLoM=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321738; v=1; x=1796926538; b=dA7oh8qKMo6dQPZCyEb3MBic5aSPxpC+Tx0umeeaN4znK+Y56cCiIz2N7Vh+p5W38KxcWCgA 8mNFZrQ36b6kheyqwhGjH9kvf2MSTItwYpKBVoN1UB92lk8VoruN9Ngpke5bzto/TbIl73R4Wde ZnpCTa4lBvmzvAc8ftuKGQ5o= X-Envelope-To: linux-mm@kvack.org Received: by mta12.migadu.com with ESMTPS id 749a9a885b611f9a; Thu, 03 Dec 2026 18:15:38 +0000 X-Mizu-Trace-ID: 749a9a885b611f9a 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 20/32] mm, swap: charge an xswap entry when it gets physical backing Date: Fri, 4 Dec 2026 02:13:07 +0800 Message-ID: <20261203181319.901844-21-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: 56D6D1A000C X-Rspamd-Server: rspam03 X-Stat-Signature: 38fgzpfwqee5iy614pizhnphikfk49w1 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-Rspam-User: X-HE-Tag: 1796321740-370383 X-HE-Meta: U2FsdGVkX1+cAWBxE4A02Nv6NEZzX0zmjiG687JrvMHof5rC/9dmGQFsA1d/uQmQ/3NBDHRDsMjYNKKk5ZunjucEJcBFM83qp1nV468DL38JZB064SJvaRUdi0+6rOFmVtF65Wgh2SVHnvEDUuesZSdPvoHFFjhhlZkE2/54yb7B+MUzEpT4Ny8nsPl4/FvqZWu7AbwrKO3UJC2DUNoTenSWhH93wCODc5FR6SH4MxKv0yTHf7wzUIz8nQBY8NZ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Pdhn1pO9XNvnqhYeio3aiend7tA4f6p0zobqDBhHmr5Zr1SHk19dOPnJ2lcRsvL5uV+mvasVbLWpAn8j4cCaAmHJhQbHf6OmoKnMbrpECQNJxMd0s7MktJTLtjCo2I8cu31E4IWJGfpK8OjsJwgnvvTux5CkPXBA4Yw== 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: Nhat Pham An xswap entry is recorded but not charged at allocation. Charge memcg->swap when the entry takes a physical backend slot, in xswap_backend_alloc(), and uncharge it when the backend is released. memory.swap.current therefore still counts the pages that do reach the disk, and memory.swap.max is still enforced on them. Suggested-by: Johannes Weiner Signed-off-by: Nhat Pham Signed-off-by: Baoquan He --- mm/swapfile.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 313aa2af0b73..02724de7811a 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -103,6 +103,11 @@ static void xswap_clear_cache_only(struct swap_info_struct *si, unsigned int start, unsigned int nr); 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); static int xswap_create(int prio); static int xswap_create_by_name(const char *name, int swap_flags); @@ -3439,6 +3444,8 @@ static void xswap_release_slot_backend(struct swap_info_struct *si, psi = swap_type_to_info(swp_type(phys)); if (psi) xswap_free_phys_slot(psi, entry, phys); + + xswap_backend_uncharge(entry, 1); } /* @@ -4896,6 +4903,7 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order, struct swap_cluster_info *ci; unsigned long nr = 1UL << order; unsigned long off = swp_offset(entry); + unsigned short id = 0; swp_entry_t phys; unsigned int i; @@ -4927,7 +4935,21 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order, * The slot is a shadow until the reverse map is installed below. * Physical swapoff can see it and retry; the window is short and * try_to_unuse() reschedules. + * + * The xswap entry was only recorded at allocation, so charge the + * physical swap here. On failure, drop the physical slot range without + * uncharging it. */ + ci = swap_cluster_lock(si, off); + if (ci) { + id = xswap_entry_id(ci, off); + swap_cluster_unlock(ci); + } + if (mem_cgroup_swap_charge(id, nr)) { + __xswap_backend_unlink(entry, phys, order); + return (swp_entry_t){}; + } + /* One IO reads a large folio, so its range has to be contiguous. */ VM_WARN_ON_ONCE(swp_offset(phys) % nr); VM_WARN_ON_ONCE(off % SWAPFILE_CLUSTER + nr > SWAPFILE_CLUSTER); @@ -4957,8 +4979,32 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order, return phys; } -/* Undo xswap_backend_alloc(): the zswap copy is still in place. */ -void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order) +/* The ID that owns an xswap entry, or 0 if it has no owner recorded. */ +static unsigned short xswap_entry_id(struct swap_cluster_info *ci, + unsigned long off) +{ + 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. */ +static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys, + unsigned int order) { struct swap_info_struct *si = __swap_entry_to_info(entry); struct swap_info_struct *psi = swap_type_to_info(swp_type(phys)); @@ -4987,6 +5033,13 @@ void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order) } } +/* Undo xswap_backend_alloc(): the zswap copy is still in place. */ +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, unsigned long start_idx, unsigned long nr, bool link_free) -- 2.54.0