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 39D29606A33E for ; Thu, 3 Dec 2026 22:01:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lPU+phXv Received: by kanga.kvack.org (Postfix) id 1CF626B05BE; Thu, 3 Dec 2026 17:00:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 181836B05BF; Thu, 3 Dec 2026 17:00:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC76F6B05C0; Thu, 3 Dec 2026 17:00:42 -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 C29A16B05BE for ; Thu, 3 Dec 2026 17:00:42 -0500 (EST) Received: from smtpin05.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4C371120188 for ; Thu, 3 Dec 2026 22:00:42 +0000 (UTC) X-FDA: 85504041924.05.EAEE59E Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) by imf16.hostedemail.com (Postfix) with ESMTP id 7A68F180011 for ; Thu, 3 Dec 2026 22:00:40 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=lPU+phXv; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.169 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796335240; b=IE8KzN0eFPiZ4HGBjtjejn1ci4MEqUvZZZm9IUbPshyg+hcHFHYQJxgws9KyVou6OuHQee n74HTHiQqUIdvYEQ1J9mtrf/fX3Bw4F540AR6NmzL0smwzsOkIQ+5lsdfMFhrXERTzs38S PEWAt/C92QYbvtU15EZl11r27RlgouQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796335240; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/BeyLdN/6gbc0IU4uyZ0pvRJ2wIxoL5vensV6B0VioQ=; b=y+8Dw2dCxDxg6S25zWFmSNvXu3zapla4GoMwLlzzcSvF+onRk+YJKuXAssQwmc7wzC+KPf Vy5cHGUKnhrBhHTVQ10wJy6fTYFfhmtIuGaFvBxOYKP0fHl1B+yY5MAuxdLknTRH5tqbpU l8hKEW4uK70Xu60SFa091/yUGJ/WUvM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=lPU+phXv; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.169 as permitted sender) smtp.mailfrom=nphamcs@gmail.com Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-8b4c685c751so2497207b3.1 for ; Thu, 03 Dec 2026 14:00:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335239; x=1796940039; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/BeyLdN/6gbc0IU4uyZ0pvRJ2wIxoL5vensV6B0VioQ=; b=lPU+phXvYMs3Gw+blMoFO8y+jQzNlnIAgbpicDUR+s9x6uuH103Qe8DaZ6bzgCv1/v Fi40rlOcUxInUaIUqn5W5/WBapr33cucZ+HsAEx53uWwAyCj9CLlzRraSmW9SU7pH7Wr +FVPjf6Bv8pttvSwboXp12BTxIU7K7Wxl1c+BqGOFQG1gtji/WrWR8xK8/HPCpM6u47g 4bqSXfR0vLSorbVpoeXoSOaW0fctVJIeSXpupQmuxeVjLiOKGABY/o3Laezr+xbdPvS0 GEWhef53f0tm4S32JGn2CbruqgTRztR8403cK9hPOacxTklWOjYZw4tYT3PsiSoVWWLo CLwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335239; x=1796940039; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=/BeyLdN/6gbc0IU4uyZ0pvRJ2wIxoL5vensV6B0VioQ=; b=Nw9KlS7+kqKOX/YorXaQPzH7PKFCuIb0LxZLAxXD3NYetQtynf1oU7DtHCrogPzdQq UDNv5Xn7TJc9hpzu8fau03+Gk+HarlVYckzHMqZ9FaEHS/T/8vKFmqhQuhbYn3TKNOGQ 0pA+Qy2TACBYEx4xWn9khestevrAnT4kcjxswx800SADb+zt4ESWmu3bQ2cu94gedgnU WTKSQzH2vv20/wBUh7BDBmc1TD2PuGFF/5n9UmxlOFe7gA7xUmQBdJL7z3Youx2lg29A JE16K8Ll8uHHR4i5fWP98gGe0I/QW97fUqjAgh+HFUeL8rwNd8FvoErLYTN4elrc9l5m 1ZCA== X-Gm-Message-State: APuU4Z+vMN8uRhMU0VVuQWhd+S8jVLzi+pSIWhEcHLiz9l+gFSOq0t8V qepz9Vhnn9N0bp9/DXoMwxXi6HhLblSO5aN4Er4vDUw4icZp/3ulZ5npiLs5pG3I X-Gm-Gg: AZFdTZfHO6oGCR8OxAWuIhRuaLu4VW+7/saDEuarYu+G091ogXSKGBXxIgysJn4SmsF Mn2W9FdfauNk0XnhjgM1GzCY0Wf/I7jVKB/mujGKqqEQMqS7Mw6Uqw1jpozIoulMCVHqKFlGC3e V05sB8bb7lAIrr5wuafV/zyPwOc1ZW0q7JPoQAX5oF2hDyPobzqp4cgpaszxtxZmsnj5f8r9eVu zyFm+jVg/uAvrqqZKanv2A9OO0YBsTvCmxhd4koV23eH9emT+7y2sXZ+YtlUNm1lJQSQfJC/Ebb bLS7N8jxqVVxIfg6kYvSZUsol1PhaBVvpjxcKVa4RI1tnnNRuRkYeDxPEkTr80YgVZAUOqfujKV vDMNLGf8XbCwVk6FOGEMFDWz2hCXTTQq2Dh35w+w3S0F6jm7FJNMYArNFRI0IYaPR3gKhe7xjTi Sz30xuKvLGBFpj/07qDTyojLRYUXOo+Y8RZFAx/9amo562TPSBIRmnzBovyKmOHlfbztfLeK63T AjZmyFkudv/K8p01CosMYYbkpH3LE2yO2JbDgPDq1Uy1wtwUuvOZArl44ZQojiu2ZkjDVBhBH2t X6SDkduOsA1WNpGmHZOnp4+gFp0r04s2RLSixAcY/Roq5giOLme5C2bnAwwJCa8B5zYBY2Qzp0N 0olp68CFGaWKHXBk64O6XCQSL2cQ2GjcnDjbOmA== X-Received: by 2002:a05:690c:287:b0:8b5:d802:58e4 with SMTP id 00721157ae682-8b660626d22mr1249377b3.24.1796335238685; Thu, 03 Dec 2026 14:00:38 -0800 (PST) Received: from localhost ([2a03:2880:10ff:73::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660df4977sm987747b3.37.2026.12.03.14.00.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:38 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.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 , "Borislav Petkov (AMD)" , Jeff Layton , linux-doc@vger.kernel.org, cgroups@vger.kernel.org, Tejun Heo , =?UTF-8?q?Michal=20Koutn=C3=BD?= Subject: [PATCH v7 14/20] mm, swap: defer memcg_table allocation for physical swap clusters Date: Thu, 3 Dec 2026 14:00:04 -0800 Message-ID: <20261203220010.1302207-15-nphamcs@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: zns7kxg3cd41c6m7s8shme6zowm5nbxy X-Rspam-User: X-Rspamd-Queue-Id: 7A68F180011 X-Rspamd-Server: rspam12 X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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-HE-Tag: 1796335240-617660 X-HE-Meta: U2FsdGVkX19Xcs7erp2mvMDIzjsCM7ZBOqN5A9o6O6TXnRAkVzVhSZPJznMNPUjMOuYQ5fga4s8LOlrAaQj0xsuhaEyWZe2RRmjKRqt69b9GY5pe45bV0wQKJbDzkymSJ+OuBSPQutmOmG79ExkXjyA9wqbMUeDwwh61KK7XR3iTdnnfZBzUSixj/pSPU2LNM1allStAK7Qo5T86RUZW4jWTdWJOv9RWzFQdHdQfE6NCEER5eEbFm86OTad+Ck0mUKacUrk9UtXjswXeq/sD0fdjOe6rYj8lOGBpcOaCVNVz1raJUIGg/sd6WcuMHg8MDLH144CcpWwAw1LOxFYMLNOo7KxcIzNp6xhkOmneIrkuehKukbeuZv4inlz1V0NO0c0blK/FagJBO6Ca6jwoN9zQQbhztsSqzXbYiu6y07F0CVduciCc3sVBPYFCMFpXp6A3r0qP67wc+qfCwF+pQtZSQyzbHCPQVte3DgwQFo3e0TUKVkfB2DiukCnbWfQjjQxAxnuWGgLloJdCsWO9Dx0ub7tFzZpk3SVHvV8y1iuFunKpJBk2j6jpxFi0VXZxQpKlPRyegPVo/t7rb58e0d5CR4lP4ptvUG0JCdIGnLA4sG5mmRlwm88xH5Z1/4suc1uqW41Pn2eL/r7JuVJSLDbf1RN3QipJXQMvrUisDUhlZucOvw92PDQXM/j6JGL+NsWPxHtZCqKrpGS8jRoXu/n41yQLApOt/RMVocRhDCLp4/YqD83vM2DdhW5Qm9pcRzYAWyiJt2i4/JZk7N7d2es2mrJxUA3JJNigwciiQJv1PtJTPWIemC7OKJi4udq8GU7h73f3NmXqPKHg2Sz1Sa597tjMsJMAzc1ehD3Jzd5NQXtX+n75F122UvR6HdrMYPtslCfNkrZMbCo8Mjd5xcDXs7QlPbV4eBT4XTZ46GAUIt44kVrIzsZ5uA6Yax5vmy6uAYvDTbbhGIl8VFU BpLfzDVI T1oBqhutELGSQH9UJarsmnUGqUq07XNO7O6HkeClv19v+jG/34MSgLL3dPZPe5/J9SXMtFkWKEQWLiymE8g7MIZu2NTGkog2vb5NBCTzrwp/G30wfkaHCfNmZ877PmC1UfHDV0H5068Sa3hM4qQiotRzd8Z2tEnfbd/DZrWW2fDImoJT2e/Pse/N2pXvh4dBqVGHhAL1JqlJRmyi0CqcQNM7zGVnlwDpp4u+LqM/Q0AgtEs1IAnHomDBGfSCnDDBesuPLb/o2aKJ94Zi9iv+Xa1QJWEXNuY8lVnROiGrE3PsZdeEIBiESI3SjnOUF/2EFBofbwygegVheUNjiufFdf0+VAntZLGPP/DJynJqC7aODXG6k2Q8j1D8v7CCIyFWbuL7Sz10x4h+gx5YdALk6fvun9odJ01nXdukHzbFA2S0KuydiIvDkaEqIwqrd7WuSIqlkgDO9bbgcwcGdL1elTPrtNhesso0h3KUiYEPOKa/wm5pXDWedoZ3ydTpusAllIz4R/xlCIqKAzrPnAfQmft9elCmM1579WnDT06uUSVZvwkXdh1wb2oPZMciuY8SKyxvxCrCVTMC5me6RS6x5XoxsJiDEyfmvL6SfKFLGNzYRhmz7PnhpZDFwLyxJwYV4XojECgFZQOgu7WNFP34F5M8ruYJ67Vu7nqAjxhTamo5MhTae2FVBAK9opd/lDkvYJHrAu5AqXOPAya/JIRXAOioFQLzqID31+lsnruJXucosHjr27qA+Y1y3jHfhY2uq0xUX 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 Stop allocating a memcg table for every physical swap cluster that only ever holds vswap backings. The table costs SWAPFILE_CLUSTER * sizeof(unsigned short) per cluster, 1 KB per 2 MB of swap on a 64-bit kernel with 4 KB pages. Such clusters never have their memcg_table read or written: vswap-layer charging records on the vswap cluster's table, not the physical one. Allocate eagerly for each vswap cluster and each new direct-use physical cluster. A physical cluster otherwise allocates its owner table on the first direct-use slot and skips it if all slots hold vswap backings. If the existing cluster has no owner table, its locked swapcache folio pins the new slots while allocation sleeps. Free the reservation if allocation fails. Hibernation prepares the table before publishing a raw slot. Link: https://lore.kernel.org/all/sashiko-outbox-181084@kernel.org/ Signed-off-by: Nhat Pham --- mm/swapfile.c | 127 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 104 insertions(+), 23 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index a1eef85b9eb6..30c1eed8adfe 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -481,12 +482,13 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci) * physical bad-slot setup passes NULL because the device is not published. */ static int swap_cluster_alloc_table(struct swap_info_struct *si, - struct swap_cluster_info *ci, + struct swap_cluster_info *ci, bool direct, gfp_t gfp, bool locked) { struct swap_table *table = NULL; #ifdef CONFIG_MEMCG struct swap_memcg_table *memcg_table = NULL; + bool vswap = si && swap_is_vswap(si); #endif #if !SWAP_TABLE_HAS_ZEROFLAG unsigned long *zero_bitmap = NULL; @@ -505,8 +507,19 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si, goto lock; #ifdef CONFIG_MEMCG - if (!mem_cgroup_disabled()) { - memcg_table = kzalloc_obj(*ci->memcg_table, gfp); + /* + * Backing-only clusters keep quota owners on the virtual cluster. + * Physical owner metadata is prepared on demand for direct use + * and physical-slot statistics. + */ + if ((!vswap_is_enabled() || vswap || direct) && + !mem_cgroup_disabled()) { + gfp_t memcg_gfp = gfp; + + if (direct && !vswap) + memcg_gfp &= ~__GFP_NOMEMALLOC; + + memcg_table = kzalloc_obj(*ci->memcg_table, memcg_gfp); if (!memcg_table) goto lock; } @@ -601,7 +614,7 @@ static void swap_cluster_assert_empty(struct swap_cluster_info *ci, */ static struct swap_cluster_info * swap_cluster_populate(struct swap_info_struct *si, - struct swap_cluster_info *ci) + struct swap_cluster_info *ci, bool direct) { int ret; @@ -616,7 +629,7 @@ swap_cluster_populate(struct swap_info_struct *si, lockdep_assert_held(&si->global_cluster_lock); lockdep_assert_held(&ci->lock); - if (!swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC | + if (!swap_cluster_alloc_table(si, ci, direct, __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN, true)) return ci; @@ -630,7 +643,7 @@ swap_cluster_populate(struct swap_info_struct *si, spin_unlock(&si->global_cluster_lock); local_unlock(&percpu_swap_cluster.lock); - ret = swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC | + ret = swap_cluster_alloc_table(si, ci, direct, __GFP_HIGH | __GFP_NOMEMALLOC | GFP_KERNEL, false); /* @@ -713,7 +726,7 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info * this returns NULL for an non-empty list. */ static struct swap_cluster_info *isolate_lock_cluster( - struct swap_info_struct *si, struct list_head *list) + struct swap_info_struct *si, struct list_head *list, bool direct) { struct swap_cluster_info *ci, *found = NULL; u8 flags = CLUSTER_FLAG_NONE; @@ -745,7 +758,7 @@ static struct swap_cluster_info *isolate_lock_cluster( if (found && flags == CLUSTER_FLAG_FREE) { VM_WARN_ON_ONCE(list != &si->free_clusters); VM_WARN_ON_ONCE(cluster_table_is_alloced(found)); - return swap_cluster_populate(si, found); + return swap_cluster_populate(si, found, direct); } return found; @@ -920,7 +933,7 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si, need_table = !ci->count; /* Need to allocate swap table first for initial bad slot marking. */ if (need_table) { - ret = swap_cluster_alloc_table(alloc_si, ci, GFP_KERNEL, false); + ret = swap_cluster_alloc_table(alloc_si, ci, false, GFP_KERNEL, false); if (ret) goto out; } else { @@ -1173,7 +1186,9 @@ static bool swap_prefer_free_cluster(struct swap_info_struct *si) /* Try use a new cluster for current CPU and allocate from it. */ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci, - struct folio *folio, unsigned long offset) + struct folio *folio, + unsigned long offset, + bool *nomem) { unsigned int next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID; unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER); @@ -1202,6 +1217,19 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, if (!ret) continue; } +#ifdef CONFIG_MEMCG + /* Hibernation needs owner metadata before publishing a raw slot. */ + if (vswap_is_enabled() && !folio && + !mem_cgroup_disabled() && !ci->memcg_table) { + ci->memcg_table = kzalloc_obj(*ci->memcg_table, + GFP_ATOMIC | __GFP_NOWARN); + if (!ci->memcg_table) { + if (nomem) + *nomem = true; + goto out; + } + } +#endif if (!__swap_cluster_alloc_entries(si, ci, folio, offset % SWAPFILE_CLUSTER)) break; found = offset; @@ -1211,7 +1239,15 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, break; } out: - relocate_cluster(si, ci); + /* + * On a discard-capable device, relocating a cluster whose memcg_table + * allocation failed queues a discard for slots that were never used, + * which folio_alloc_phys_swap() reads as progress and retries on. + */ + if (nomem && *nomem && !ci->count) + __free_cluster(si, ci); + else + relocate_cluster(si, ci); swap_cluster_unlock(ci); if (swap_is_vswap(si)) { this_cpu_write(percpu_vswap_cluster.offset[order], next); @@ -1232,18 +1268,25 @@ static unsigned int alloc_swap_scan_list(struct swap_info_struct *si, bool scan_all) { unsigned int found = SWAP_ENTRY_INVALID; + bool nomem = false; + bool direct = !folio || !folio_test_swapcache(folio); + /* + * In rare cases alloc_swap_scan_cluster() can fail due to + * memcg_table allocation failure. Short-circuit to avoid looping + * over the list indefinitely. + */ do { - struct swap_cluster_info *ci = isolate_lock_cluster(si, list); + struct swap_cluster_info *ci = isolate_lock_cluster(si, list, direct); unsigned long offset; if (!ci) break; offset = cluster_offset(si, ci); - found = alloc_swap_scan_cluster(si, ci, folio, offset); + found = alloc_swap_scan_cluster(si, ci, folio, offset, &nomem); if (found) break; - } while (scan_all); + } while (scan_all && !nomem); return found; } @@ -1268,7 +1311,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si, ci = &ci_dyn->ci; spin_lock(&ci->lock); - if (swap_cluster_alloc_table(si, ci, GFP_ATOMIC | __GFP_NOWARN, true)) { + if (swap_cluster_alloc_table(si, ci, false, GFP_ATOMIC | __GFP_NOWARN, true)) { spin_unlock(&ci->lock); kfree(ci_dyn); return SWAP_ENTRY_INVALID; @@ -1292,7 +1335,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si, } offset = cluster_offset(si, ci); - return alloc_swap_scan_cluster(si, ci, folio, offset); + return alloc_swap_scan_cluster(si, ci, folio, offset, NULL); } static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) @@ -1307,7 +1350,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) if (force) to_scan = swap_usage_in_pages(si) / SWAPFILE_CLUSTER; - while ((ci = isolate_lock_cluster(si, &si->full_clusters))) { + while ((ci = isolate_lock_cluster(si, &si->full_clusters, false))) { offset = cluster_offset(si, ci); end = min(si->max, offset + SWAPFILE_CLUSTER); to_scan--; @@ -1394,7 +1437,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, if (cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset = cluster_offset(si, ci); - found = alloc_swap_scan_cluster(si, ci, folio, offset); + found = alloc_swap_scan_cluster(si, ci, folio, offset, + NULL); } else { swap_cluster_unlock(ci); } @@ -1434,7 +1478,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, if (order < PMD_ORDER) { /* * Scan only one fragment cluster is good enough. Order 0 - * allocation will surely success, and large allocation + * allocation rarely fails, and large allocation * failure is not critical. Scanning one cluster still * keeps the list rotated and reclaimed (for clean swap cache). */ @@ -1449,7 +1493,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, /* Order 0 stealing from higher order */ for (int o = 1; o < SWAP_NR_ORDERS; o++) { /* - * Clusters here have at least one usable slots and can't fail order 0 + * Clusters here have at least one usable slots and rarely fail order 0 * allocation, but reclaim may drop si->lock and race with another user. */ found = alloc_swap_scan_list(si, &si->frag_clusters[o], folio, true); @@ -1670,7 +1714,7 @@ static swp_entry_t swap_alloc_fast(struct folio *folio) if (ci && cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset = cluster_offset(si, ci); - found = alloc_swap_scan_cluster(si, ci, folio, offset); + found = alloc_swap_scan_cluster(si, ci, folio, offset, NULL); } else if (ci) { swap_cluster_unlock(ci); } @@ -2058,7 +2102,8 @@ static bool vswap_alloc(struct folio *folio) if (ci && cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset = cluster_offset(vswap_si, ci); - alloc_swap_scan_cluster(vswap_si, ci, folio, offset); + alloc_swap_scan_cluster(vswap_si, ci, folio, offset, + NULL); } else if (ci) { swap_cluster_unlock(ci); } @@ -2079,8 +2124,37 @@ static bool vswap_alloc(struct folio *folio) return false; } +#ifdef CONFIG_MEMCG +static int swap_cluster_alloc_memcg_table(swp_entry_t entry) +{ + struct swap_cluster_info *ci = __swap_entry_to_cluster(entry); + struct swap_memcg_table *table __free(kfree) = NULL; + + if (mem_cgroup_disabled() || READ_ONCE(ci->memcg_table)) + return 0; + + /* The caller's locked folio pins the slots while allocation sleeps. */ + table = kzalloc_obj(*table, GFP_ATOMIC | __GFP_NOWARN); + if (!table) + table = kzalloc_obj(*table, __GFP_HIGH | GFP_KERNEL); + guard(spinlock)(&ci->lock); + if (ci->memcg_table) + return 0; + if (!table) + return -ENOMEM; + WRITE_ONCE(ci->memcg_table, no_free_ptr(table)); + return 0; +} +#else +static int swap_cluster_alloc_memcg_table(swp_entry_t entry) +{ + return 0; +} +#endif + static swp_entry_t folio_alloc_phys_swap(struct folio *folio) { + bool direct = !folio_test_swapcache(folio); swp_entry_t entry; again: @@ -2092,7 +2166,13 @@ static swp_entry_t folio_alloc_phys_swap(struct folio *folio) if (!entry.val && !folio_order(folio) && swap_sync_discard()) goto again; + if (!entry.val) + return entry; + if (direct && swap_cluster_alloc_memcg_table(entry)) { + swap_cache_del_folio(folio); + return (swp_entry_t){}; + } return entry; } @@ -2895,7 +2975,8 @@ swp_entry_t swap_alloc_hibernation_slot(int type) if (pcp_si == si && pcp_offset) { ci = swap_cluster_lock(si, pcp_offset); if (cluster_is_usable(ci, 0)) - offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset); + offset = alloc_swap_scan_cluster(si, ci, NULL, + pcp_offset, NULL); else swap_cluster_unlock(ci); } -- 2.53.1-Meta