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 E0668606A322 for ; Thu, 3 Dec 2026 18:16:15 +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=ZVklFPKp Received: by kanga.kvack.org (Postfix) id F38BB6B05DD; Thu, 3 Dec 2026 13:16:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EE9B06B05DE; Thu, 3 Dec 2026 13:16:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFFA96B05DF; Thu, 3 Dec 2026 13:16:14 -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 C0FD46B05DD for ; Thu, 3 Dec 2026 13:16:14 -0500 (EST) Received: from smtpin23.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5CE92160803 for ; Thu, 3 Dec 2026 18:16:14 +0000 (UTC) X-FDA: 85503476268.23.F842E19 Received: from mta0.migadu.com (out-114.mta0.migadu.com [91.218.175.114]) by imf02.hostedemail.com (Postfix) with ESMTP id 393C280012 for ; Thu, 3 Dec 2026 18:16:11 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ZVklFPKp; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.114 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=1796321772; b=CNJTAfU+RdcBUQCP+oJqoFvG4MTyG7GHROdBeAbWkEwlTQoc9Pte63AzwH/jENGL6upQ7C tLORl3z5nrGGwIzqKHXaNSoQIdZEAkD+rn2ke2wQbbIFJ1bG9oaJKtyPVOory4rd5tCv0b qDN7Bqi7aYpfc30J+Gsf6KYHXzq5FcM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321772; 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=iwBsO1xN+/DQTHeZgfA4Gp6DHkiMNxAfLa6dX+mDzjM=; b=Y79opsZbwcs5HUPT32FuQqABR6iRVJ4vEX4AEFKPyWIm9T+SAHJ6nMIMCnIbsK0VqiqSTs QuBLq84xdULZR6Fq6UKc/T6bYX5yPMtAJUBLiN93H7A4GofurQkGurDrK8E6EdIlCm7LOr 3rzRRagsiCOS3oX9DHupg3jYrgeI2M8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ZVklFPKp; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.114 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=F7uVo4842qMPYaypEovEYnwDF47R/OyLW3l+lEJtUGw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321770; v=1; x=1796926570; b=ZVklFPKp1Op25w3D5qWrgvvy4c+XkO1Hr1kvv/eDrMPig9UqrjSiLXfb5T6cC/HD8ptpWvU5 2cUmpWBYnQZeQBsT9ywmG3I171OIrPLHgk29tDWd9PhXsa1zNzkkwaGQ4J4A1bsfo3oocoYG8Rs Oz5RkRiVWY7uwrTtdntEAo3I= X-Envelope-To: linux-mm@kvack.org Received: by mta11.migadu.com with ESMTPS id e1d84676fd21e398; Thu, 03 Dec 2026 18:16:10 +0000 X-Mizu-Trace-ID: e1d84676fd21e398 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 25/32] mm, swap: drop a refused xswap backend slot range directly Date: Fri, 4 Dec 2026 02:13:12 +0800 Message-ID: <20261203181319.901844-26-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: i5gbufe5fd7k4hwgbusabf7kwkndyo8n X-Rspam-User: X-Rspamd-Server: rspam11 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-Queue-Id: 393C280012 X-HE-Tag: 1796321771-746549 X-HE-Meta: U2FsdGVkX19qQbjPAT/0fXB/r/XWvrQHfdFlMS7KGa49z+rspWQ7wzYpbWbmN0GVhnkp8lhf/LMjzLlBT1C8Iz5IAcZgArtJP3bkx4QKrl4cgrMpCAF56Ye7+xwhPA2OtNX7nnhlAjlXNnat9Ne8fafjUc4J65aZxuaaowRIkfDx2sXm4y2Vn3SU1DKAjODgJ6JLWIFuMA+fu7YIiDEOll08PaNjPEgc+e8WSn4h65hRnGyRa6Qnj0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CqtDgmh8vINrE9dZY0xObQxvQxC05SEMmZpUrpbJbe5dznc8ETr4L5aES1onK7sZzbQiyRXq+yWE4MCkWjEM/YGcRQXsbHHYBmJdDahZndhvf4qsQO 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 On a refused charge, xswap_backend_alloc() gave the physical slot range back with __xswap_backend_unlink(), which finds the slot range through the reverse mapping. That mapping is not installed yet at this point, so it recognised nothing and freed nothing: every refused charge leaked the slot range and tripped a VM_WARN_ON_ONCE(). Drop the slot range directly instead; the caller holds it and nothing points at it yet. Free the range while the cluster is still locked and before free_cluster() can hand it to another user, so the invalidate hooks cannot run against a new owner's table. Seen with a cgroup at memory.swap.max: the backend held 48663 pages while memory.swap.current was 0. Signed-off-by: Baoquan He --- mm/swapfile.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index d8f09f46123a..c6df87019df0 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3424,6 +3424,44 @@ static void xswap_free_phys_slot(struct swap_info_struct *si, put_swap_device(si); } +/* + * Give a backend slot range back. Nothing points at them yet, so there is + * no reverse mapping to look up and no record to clear. + */ +static void xswap_drop_phys_slots(swp_entry_t phys, unsigned int nr) +{ + struct swap_info_struct *psi = swap_type_to_info(swp_type(phys)); + unsigned long poff = swp_offset(phys); + struct swap_cluster_info *pci; + unsigned int i; + + if (!psi) + return; + + pci = swap_cluster_lock(psi, poff); + if (!pci) + return; + + VM_WARN_ON(pci->count < nr); + pci->count -= nr; + for (i = 0; i < nr; i++) + __swap_table_set(pci, (poff + i) % SWAPFILE_CLUSTER, + null_to_swp_tb()); + + /* + * The slot must not be allocatable before its invalidate hooks run: + * free_cluster() can hand the cluster to another user, and the + * invalidate would then walk that user's backend. + */ + swap_range_free(psi, poff, nr); + + if (!pci->count) + free_cluster(psi, pci); + else + partial_free_cluster(psi, pci); + swap_cluster_unlock(pci); +} + /* * An xswap slot being freed may still own a physical slot. Drop it, or the * physical space and its reverse mapping leak. Caller holds the xswap @@ -4956,7 +4994,7 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry, unsigned int order, swap_cluster_unlock(ci); } if (mem_cgroup_swap_charge(id, nr)) { - __xswap_backend_unlink(entry, phys, order); + xswap_drop_phys_slots(phys, nr); return (swp_entry_t){}; } -- 2.54.0