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 C9F08606A338 for ; Thu, 3 Dec 2026 18:14:39 +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=Rq/OnDQL Received: by kanga.kvack.org (Postfix) id F0E216B05C8; Thu, 3 Dec 2026 13:14:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EBE7D6B05C9; Thu, 3 Dec 2026 13:14:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DAD816B05CA; Thu, 3 Dec 2026 13:14:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B6CB36B05C8 for ; Thu, 3 Dec 2026 13:14:38 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 51E97120808 for ; Thu, 3 Dec 2026 18:14:38 +0000 (UTC) X-FDA: 85503472236.09.03FFE12 Received: from mta0.migadu.com (out-34.mta0.migadu.com [91.218.175.34]) by imf24.hostedemail.com (Postfix) with ESMTP id 4AAE8180005 for ; Thu, 3 Dec 2026 18:14:36 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Rq/OnDQL"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf24.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.34 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=1796321676; b=OHKHR86nkK3+1v44FstbeiUPhEVbsEto51eT/CB93l5Rl5Sh87A5z62yZYk1/SfaDB1cqv oJJnOEHceldzrYaK8mdzXANDXJuM4DAFP3EC8/h8Z7CB5Ya7QfwtTy7F53fsx446focZXr EGl7zr/lJRHBadloec05nfNOx8uFYL4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796321676; 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=bSzcOnF+pdAMX+xgbg5rDbyFXzBospyN6suDmDTW3v8=; b=d0HJBVwmy5DiM8lbMlhgCuCLctE9xy1p27cfV48LkQcHgk8dHxvH20AahUtdEnXjaq5UBS Cbzvr7eiWeuUYQtZ0lStdhlZw61pV8Oom7UjQJ6I1RV2gCtNeDMHVFv/06cfswnwu9Rzdg orR05+iYr2P287AjPEtAU/5+s8tQozk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Rq/OnDQL"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf24.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.34 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=zPG5KQ5bmbJWbpvOnx7XX/RgEXG0Cw3PXsBl3fS3X2k=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796321675; v=1; x=1796926475; b=Rq/OnDQL28KWc1MynoXCtSfKyDlgiyZCI4uUOkVE5zKdWaY0VEyZwcs1uhccXCQ+7/RL03wv /2boSMRDRPFxXxqSbRtHK6O1SWTKDp+xCTfMRUm3nz4sohLzctkc53ie/dKnOM16s6sEGSUU5OF zjU/8AA1pnf7wytR2StXbx/o= X-Envelope-To: linux-mm@kvack.org Received: by mta10.migadu.com with ESMTPS id 14e5fb093ad41359; Thu, 03 Dec 2026 18:14:35 +0000 X-Mizu-Trace-ID: 14e5fb093ad41359 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 11/32] mm, swap: publish the xswap backend owner before its reverse map Date: Fri, 4 Dec 2026 02:12:58 +0800 Message-ID: <20261203181319.901844-12-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: rspam04 X-Rspamd-Queue-Id: 4AAE8180005 X-Stat-Signature: frth86zzztnperdk7ksp1xqqonh5szmg 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: 1796321676-759558 X-HE-Meta: U2FsdGVkX19XxXbiXBnfR7eFfYvR1zCoF7NLHtc6VgZniKuExbjeHlI9u29aGsiZU0qbIbBE4KPSX8P2wmMRbuY81x8xw6QJSlZhFlXz1wNljWIhx+Xd8uysPip+/EmK1KKho94Nlc3Ub1QsOiGjLRMIu5mJBeujB1grgFrc5Eu/tn5Ed7yZu9KhuUh++kKd6jEQgAkSadWg/pTDymTJSjP3kFZSYbc3nyU8QT2fIz6s37IbP69qan1js/TdsJ5jRvR4f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iF8xkFsuG0XSg7/pJXM33ugfV2DZ4Vro0fqYZxF311Y2XkVzgtPfp+t3w0DXjkUMz7bYFmXpJrUtJrVksizSC4ztOOPjh+v22OSkoABI7w 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 Preallocate the backend table before allocating the physical slot, and record the owner before installing the reverse map. A slot is then never visible to swapoff without its owner. Signed-off-by: Baoquan He --- mm/swapfile.c | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index e55711300169..adc7f41cd8a3 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -4761,8 +4761,8 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry) struct swap_info_struct *si = __swap_entry_to_info(entry); struct swap_cluster_info *ci; unsigned long off = swp_offset(entry); + unsigned long swp_tb; swp_entry_t phys, old; - bool recorded = false; /* * A folio that was written back, redirtied while still in the swap @@ -4777,6 +4777,19 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry) xswap_backend_free(entry, old); } + /* + * Get the xswap side ready before a physical slot exists, so the + * reverse map installed below cannot expose a slot with no owner. + */ + ci = swap_cluster_lock(si, off); + if (!ci) + return (swp_entry_t){}; + if (!xswap_xs_table_locked(ci, GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC)) { + swap_cluster_unlock(ci); + return (swp_entry_t){}; + } + swap_cluster_unlock(ci); + phys = xswap_alloc_phys_slot(); if (!phys.val) return phys; @@ -4785,31 +4798,27 @@ swp_entry_t xswap_backend_alloc(swp_entry_t entry) * 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. + * + * Owner first, then the reverse map, under one hold of the lock. */ - xswap_install_rmap(phys, entry); - ci = swap_cluster_lock(si, off); - if (ci) { - unsigned int slot = off % SWAPFILE_CLUSTER; - unsigned long swp_tb; - - recorded = xswap_slot_set_backend(ci, slot, phys); - /* - * The folio can lose its last reference while the owner is - * not recorded yet; that path cannot mark the slot for us. - */ - swp_tb = __swap_table_get(ci, slot); - if (recorded && swp_tb_is_folio(swp_tb) && - !swp_tb_get_count(swp_tb)) - xswap_mark_cache_only(si, ci, slot); - swap_cluster_unlock(ci); - } - - if (!recorded) { - /* The backend is unrecorded, so the data would be unreachable. */ + if (!ci) { xswap_backend_free(entry, phys); return (swp_entry_t){}; } + /* The table is preallocated above, so this cannot fail. */ + if (!xswap_slot_set_backend(ci, off % SWAPFILE_CLUSTER, phys)) + VM_WARN_ON_ONCE(1); + xswap_install_rmap(phys, entry); + + /* + * The folio can lose its last reference while the owner is not + * recorded yet; that path cannot mark the slot for us. + */ + swp_tb = __swap_table_get(ci, off % SWAPFILE_CLUSTER); + if (swp_tb_is_folio(swp_tb) && !swp_tb_get_count(swp_tb)) + xswap_mark_cache_only(si, ci, off % SWAPFILE_CLUSTER); + swap_cluster_unlock(ci); return phys; } -- 2.54.0