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 7A19A606A322 for ; Fri, 4 Dec 2026 22:09:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=F4O8v3Yl Received: by kanga.kvack.org (Postfix) id 15A7A6B05BB; Fri, 4 Dec 2026 17:09:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 10B7B6B05BC; Fri, 4 Dec 2026 17:09:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC72B6B05BD; Fri, 4 Dec 2026 17:09:23 -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 B3CB76B05BB for ; Fri, 4 Dec 2026 17:09:23 -0500 (EST) Received: from smtpin01.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8BAC31A03F5 for ; Fri, 4 Dec 2026 22:09:21 +0000 (UTC) X-FDA: 85507692522.01.4FEE986 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf11.hostedemail.com (Postfix) with ESMTP id EE05D4000B for ; Fri, 4 Dec 2026 22:09:19 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=F4O8v3Yl; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796422160; b=GstvUQGVotN9RJLXxuuvEx6iZpmP6DotlQcL6qGzWlZqVqqmr4Ut0YoD/6+8AkZveyHFY3 A+u5NtVWRU+4gQ/jWnJgz2azW9QR6ALF7yLa34myExZ8iStexFMvOmv98gVtLrqSadoBXN wutCnOTIzRN8Z/tEcW9un9Cw2oY8PB8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796422160; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1YEm8YUuw1KHXwq1FqSAJYBa+tr1ECLFWxECGKjfVWg=; b=SIYv19Kq4ANR3R5l0CEpyd+7cVrmV4Q1GV3mkD9vDJyo9HTa1Sts9EsbHDUxmDxq4n+BIr QAneVyRbHDD/ROk6c4YrOLjfSyKvzO/0K5a1m6ghu9KQEX0aFpkCr/4RWg8JXULynghWmh H1aQhqv7asgZyvAlvRpI/5MrPsQ6to4= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=F4O8v3Yl; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 8C77761E5B; Fri, 4 Dec 2026 22:09:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98AC91F00936; Fri, 4 Dec 2026 22:09:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796422159; bh=1YEm8YUuw1KHXwq1FqSAJYBa+tr1ECLFWxECGKjfVWg=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=F4O8v3YlL4z/Gl1nh+3MCUDnS8LhoHgHrKlWezACc2rUAb46Q3kIF2NaPM/bcuwSj 2vjLx6RsiWi4W288hAddFq9aRlYSx0VZV4DppZPnOgg33bI2xi4UQZpJhSZZ1SRygv 3fPv26eDz4c7VKk0pi1RXyHtdUdUxH/Gxtohtn3dbss0vyn8Z3kdf4kvY2WNJMCuxt AweKiESTiOr6tW0qqNU48bnbyG64xR0ZpH3uYd75HyAVjzFpWaJZiL7Ic7nynDDvod 2e3hg6z1rEex2rJ0bvgoNGwVohtaTSKvpqyHJGAPyrbZuPmv+E1l6lnKObKAYr6gnu XBVcf6EgN6rJg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags Reply-To: sashiko-reviews@lists.linux.dev To: "Nhat Pham" , linux-mm@kvack.org Cc: "Andrew Morton" , "Chris Li" , "Kairui Song" , "Kemeng Shi" , "Baoquan He" , "Barry Song" , "Youngjun Park" , "Johannes Weiner" , "Yosry Ahmed" , "Chengming Zhou" , "Qi Zheng" , "Shakeel Butt" , "Axel Rasmussen" , "Yuanchu Xie" , "Wei Xu" , "Baolin Wang" , "Michal Hocko" , "David Hildenbrand" , "Liam R. Howlett" , "Vlastimil Babka" , "Mike Rapoport" , "Hugh Dickins" , "Jonathan Corbet" , "Shuah Khan" , "Randy Dunlap" , "Roman Gushchin" , "Muchun Song" , "Arnd Bergmann" , "Karl Mehltretter" , "Dapeng Mi" , "Marco Elver" , "Ethan Nelson-Moore" , =?utf-8?b?Qm9yaXNsYXYgUGV0a292IChBTUQp?= , "Jeff Layton" , "Tejun Heo" , =?utf-8?b?TWljaGFsIEtvdXRuw70=?= In-Reply-To: <20261203220010.1302207-7-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> <20261203220010.1302207-7-nphamcs@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 22:09:17 +0000 X-Rspamd-Server: rspam04 X-Rspam-User: X-Rspamd-Queue-Id: EE05D4000B X-Stat-Signature: 677yd9wmyun8ncy1y4f3njfkdttujta4 X-Spam-Status: No, score=-11.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796422159-973798 X-HE-Meta: U2FsdGVkX19RGBqZO6Fr4gyjrOhe329DMcenKrSJwDGA75j8prkW+Rqwp/MrrXSlly/tMr9CStTqHl+fcBfL4uC4EjnE15z8WL9YfHX5wBiLA5x8ChZ7xmEL8WhJt7V931tZM7b6adc+jIReoSxeJFMiHzE0XdkmPADfLO1i/UY1B2/dJ8EqUTDYjBWPy08608ztuVNKyBHpkLGOuOOUz4/A+3ZTfOfe2yiO6gAgbPQT7reSni4M1Nyxy6ehSePoX4FitcfJPFjfobvzinGYtn/GYvRKOCYaEabSDR8qGAv6NVrQhEC5JhP34DuzPVJQoBuuxUoRyiNkpEozitJ7K7BelgdHuFF0oeFyrupuuvqQraGq6sTOochZHl8rxk0GnJFXDsEkLgsh1t/RqHxdMlt2IVAoeGbIm84lNDoV6aHu3pCMioBLir/AA16jG/rrGvcUY0JkGtCCQMS86gd6afVH4vnX8APwhiZeJKeZzQ8Qs8r0TWx5UwUoSoVhsZhvaCHXFyx167tRr4HRvj9Vo7VZZzJqRJwv0u/eIRjn89D0fVjVRjt1oVsOzL0y23JNcBAzRsCrvhX8GNvBF3Of9QHEi2nfcooDXAOuwwtTCUqqGSWMycdzrfZhKiL+iSQRqELl1K/qkuYn1vTWlAh4u7ropz1X1p6CDIvOJMGOuFAOTiZgqW1Hm0nWnxFP3KNEox2mbz6IrvcqE7ZfUstipUKI68EvX6sRoanb2n3TTDttSy80qwGy1ZS9MgeZrvyu6yltDBhUMRhjHsYjkFumQOn629TYggyUahy5EAwg2uO0tYpl4H9hpyZt9mi1VqAkMG5YcdmX0eL7y/tBBygzXpCcSe80c4pdtUJ5hXILwd6V1XbxB4ud2Px2/U/NErQw/bkgJVYr36G6bj3Hdlu+xBMDWYCGoJwgg6qgpKAgv/7s9aibr+S7ss4UuUs8c+rvWKpgQl59C17yLrX8o9O ol1tZbER 0QBovW7g79TEFiobtcrilDysw0nIa4f8VWVuRdJjsES7hQeO14pfAI9VAmTEW1g8w+3o3BUs+sDt9vrmab5YWhqIBoyfuswoNDGumyfaWTQBCjWVo1e1G4DZ4In6SAydXdK5vp0IXxLeVZ8F2Ex7pA7yakdDukZsiHfNySCG7/KDg59qgwGNIiXjy2zddkOm0Qc84Qb0/ykBeS7v4CFdCVmUsF1e3V2nqQM1VGc4o5mI0nDvOE6SuFm6qoLpjBEoX8B31y6iXqJPkquJmSIPXpSyptREV8NEnS6rOJ8JOxNtPw7MuGHPk7rFHTAomdJ8y+MwOzef5NiL+JPSByaXB7X+Iju0YbMiDofwpOulWPc7hyw2bqkWmwnuTxfdr8heFRwgkUN/w7AAYutbeg3JGJAsfZ2mXvJiULWgNPSD77i8aQK7Hn6MZEhPJzLrjxKd1eJbn1NKUEQXi2K4/CgS+RGqQ7ayFPM3vDuQuGNP9KVk30B9mfrceV7FHza6kV3QC3MW63emgCisCyMefufSZPmDxlnJqNspdoC/fz8gPurxDcRrf1+A17LhTN8zuXQesOYNTLbMuQ+nKeULa0Du2o7ltat72baX6CuQV+WqNAm5e/OtGrUWpODkJeRh9oT7K5t5eXkOH3dr2Saw+8rVoXQbueH0Wk7jlQ1yny4A+aSLZyjav3vPnPuBAjKO8/ZB0/ujBEQiVsd+ivAFV+Sulff3i3UNXPzzavB9ykgrQcgrZLysrGOXywQg0m+qOOSm7fFwEmmMSwKPAev213hMGsHylJXJecG7YDoqJBILIsoUoW3t04B4LVdV6o1TyV3g6FcC0 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 X-Archived-By: Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mm/swap: redundant vswap cluster locks and XArray walks on swap-in hot paths - [Low] mm: unlocked gap in swap_entry_backend_has_flag() races with swapoff+swapon -- commit 3f81dcd7e1282bf4edcad334ba3c53805854c831 Author: Nhat Pham mm/swap: resolve physical backing for reads and device flags This commit adds physical swap-entry encoding and lookup helpers to the vswap vtable and updates swap-in reads, synchronous I/O, stable-write, and filesystem-reclaim checks to resolve physical backing for virtual swap entries. > diff --git a/mm/memory.c b/mm/memory.c > index 9ba6ebd581324..3eb1841530bb3 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -4754,8 +4754,11 @@ static inline bool should_try_to_free_swap(struct = swap_info_struct *si, > { > if (!folio_test_swapcache(folio)) > return false; > - /* A vswap entry holds no physical slot, so keeping it saves no IO. */ > - if (is_vswap_entry(folio->swap)) > + /* > + * Non-swapfile backends cannot be reused for future swapouts. > + * Free the swap slot unless backed by contiguous physical swap. > + */ > + if (!folio_phys_swap_backed(folio)) > return true; > /* > * Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap > @@ -4763,7 +4766,7 @@ static inline bool should_try_to_free_swap(struct s= wap_info_struct *si, > * are fast, and meanwhile, swap cache pinning the slot deferring the > * release of metadata or fragmentation is a more critical issue. > */ > - if (data_race(si->flags & SWP_SYNCHRONOUS_IO)) > + if (swap_entry_backend_has_flag(si, folio->swap, SWP_SYNCHRONOUS_IO)) > return true; [Severity: Medium] Can these swap-in hot paths avoid repeating the cluster_info_pool XArray walk and ci->lock acquisition on the same vswap slot? In should_try_to_free_swap(), which runs on every do_swap_page() fault under vmf->ptl and folio_lock, folio_phys_swap_backed() locks the cluster and discards the resolved physical entry, and the next line calls swap_entry_backend_has_flag() to walk and lock the same cluster again along with get_swap_device()/put_swap_device(). The same duplicate lookup happens in do_swap_page() on non-zswap major faults, where swap_entry_synchronous() and swap_entry_backend_has_flag() walk and lock the same cluster back-to-back, and in swap_read_folio(), where vswap_to_phys() reads the vtable before swap_read_folio_zeromap() and zswap_load() look up the same slot again. Returning the resolved physical entry from folio_phys_swap_backed() and dispatching once on the decoded vtable entry in swap_entry_synchronous() and swap_read_folio() would avoid the extra spinlock contention and vmf->ptl hold time. [ ... ] > diff --git a/mm/vswap.h b/mm/vswap.h > index f64b1543e01fc..c4f0670c9b5c0 100644 > --- a/mm/vswap.h > +++ b/mm/vswap.h [ ... ] > +static inline bool swap_entry_backend_has_flag(struct swap_info_struct *= si, > + swp_entry_t entry, > + unsigned long flag) > +{ > + struct swap_info_struct *phys_si; > + swp_entry_t phys; > + bool has_flag; > + > + if (!swap_is_vswap(si)) > + return data_race(si->flags & flag); > + > + phys =3D vswap_to_phys(entry); > + if (!phys.val) > + return false; > + > + phys_si =3D get_swap_device(phys); > + if (IS_ERR_OR_NULL(phys_si)) > + return false; > + > + has_flag =3D data_race(phys_si->flags & flag); > + put_swap_device(phys_si); > + return has_flag; > +} [Severity: Low] Can the unlocked gap between vswap_to_phys() and get_swap_device() race with swapoff and swapon and trigger spurious "Bad swap offset entry" errors? In do_swap_page(), swap_entry_backend_has_flag() is called without holding folio_lock, vmf->ptl, rcu_read_lock(), or a reference on the physical swap device: CPU0 CPU1 do_swap_page() swap_entry_backend_has_flag() vswap_to_phys() vswap_lock_cluster() vt =3D __vtable_get() swap_cluster_unlock() swapoff() try_to_unuse() __vswap_release_backing() synchronize_rcu() swapon() /* smaller, same slot */ get_swap_device(phys) offset >=3D si->max <-- logs "Bad swap offset entry" Reading data_race(__swap_entry_to_info(phys)->flags & flag) while still holding ci_dyn->ci.lock avoids both this race and the get_swap_device()/put_swap_device() round-trip, since try_to_unuse() must take ci_dyn->ci.lock to release the backing and swap_info[] slots are never freed. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203220010.1302= 207-1-nphamcs@gmail.com?part=3D6