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 1C22A606A322 for ; Thu, 3 Dec 2026 22:01:47 +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=coLdCQ9x Received: by kanga.kvack.org (Postfix) id 441126B05C6; Thu, 3 Dec 2026 17:00:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F0EB6B05C8; Thu, 3 Dec 2026 17:00:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 293286B05C7; Thu, 3 Dec 2026 17:00:56 -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 D35266B05C5 for ; Thu, 3 Dec 2026 17:00:54 -0500 (EST) Received: from smtpin16.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2CE58160199 for ; Thu, 3 Dec 2026 22:00:54 +0000 (UTC) X-FDA: 85504042428.16.52B63CF Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) by imf31.hostedemail.com (Postfix) with ESMTP id 440A920006 for ; Thu, 3 Dec 2026 22:00:52 +0000 (UTC) Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=coLdCQ9x; spf=pass (imf31.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.171 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796335252; b=yJ0lavLuZhRfi1z+J13zTWwbUxxGjcKweCHQyKRXZ2D2DxaTX0v72nXq2OzMnN78xNXrNY I2pmzEVzcv89gwMX7NjkwKPImLOhD6TzFi4VMAXaglAS71W5+mhwssbTed1/HQXYkwBTQV XH3bvqaGzUMrVXRT62TL90MFv4bvxp0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796335252; 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=A39D+kDBAeXNAD5MT0EfM+MBML0hCWMWGQ5cr4dNF0I=; b=luoQIQ3SOXsgeoMakyl+cVs0wlNyl58qmQyMwjvN9kYEFDnQBsLjiozQA6SnYungzFZHcK Ie1M+5ZFhjurlONPj6DUyTwolzB3xnqI8apniNm3Um191MSwrCCmcMI+ERQfXVXgM5Vrki Kj48QyceWSCpI+0FUtKusTn36jDY1Wc= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=coLdCQ9x; spf=pass (imf31.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.171 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-8b360828905so2676857b3.2 for ; Thu, 03 Dec 2026 14:00:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335251; x=1796940051; 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=A39D+kDBAeXNAD5MT0EfM+MBML0hCWMWGQ5cr4dNF0I=; b=coLdCQ9x+73Yu1FMiiyHb+KnxFcTjG2XHd+W8fsoeWnzrHCvplPiLYwOruOSVKubt8 /hZytqmdGPgAkrqtK10QCdw/qS1hsHfQbNT56Undw3tFg1DryF28eb36lGjshjNaa2Sj BcPzXfVUY0cmXVd+48bOMq8RgjkiG3r59UZj7cDXJBt4IjB24QDWVQLwsilmxis/ySCm DTguRItQwPFR1kPkQHusjiOXRiOFhttV/yyzKmb6PBUczj/5P8aNu6/ATjRSOdop6mb7 EFTEdDv3cIFGG2D1RrnHDuTZlZcOW5xAvk1Ag+cEZpc6hzAcXHzYpuToFh29Zk2SEGjR G7/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335251; x=1796940051; 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=A39D+kDBAeXNAD5MT0EfM+MBML0hCWMWGQ5cr4dNF0I=; b=MeBBmO/c2P1AGU7SjUVMpDf30ydOPgqeV/7NK3T/4iYa22NOLA37aOOeN0v+K8ULiu SYY3J+QkU5/rgtpIQ6YJKpfsFvH78XoPCMxh7UqmS7XJ4vhfLTuFOesvnh82DX/u/qCR SpPddkcL8zwR/n6e3CQwMQX3GNbulJf9P+Ts6RQBe3Z2Tp3d8zl/8y+36jz7JvCDwOyo YJdgMSKLJr4B1OCNafYx61OIDLye0bTMIKzIO0uzSyelRuMqNjzemJ4tpTwwkmLVMxLY Dnewqil+6PP9+vW+BiA/X6i/prEvkm08A3eKBtxxDsdUtHkd4ooKHAGCSNlcHrilWGbR oX1A== X-Gm-Message-State: APuU4Z8qWT5ImaIgEnDd6U6+StRTC6VnajNb0Q4AUeiA0YjKWm78ebcd 8/qZt0isI1L3cqq79MBVkfT0EYe1hWocFnbEDWb+/ijN3t5LdfBEh5SEjdaJBPvl X-Gm-Gg: AZFdTZdmV3TMqW2P9ZtN4JoI8pdhwIXR54ytEGtmTv8fSMVJhoc4fMs/1fzbx/qdASJ 3TrnYxGmw4yLb0MlsFZLKigez4SLjzfpldLEKGCRTp760csCL5PNlDgff9LUsG3f6/s3MKzfkLW qUwRGy4UA1IdviRDxANacv+amjl0Iz16twYjS6CpsIxQNAe8fSwERTo0y9+c9T4ZrMbT83AREFu 6eM7omkLofruNJLwcPll8F8LajssmozNQsrBUNreuKUlCDtiJdr6v2apEya4mRO84NLjpU9Cs38 X+9PUfGj+tqOSu0W5ykg+ZfKynBrnXJAtUR9SK2uNfBT43Eqb7mzkUPlr49vX5aMBu75qmkvfSr xibrd1cNtt+n0jPUK5/3xxtaSP7AUuRKNUgRJfouvh79uUa5yBnZjBhVWWVg2nfqSTyyVgkKgVL VslvwPMWWRgEempdSbWA+xV9/mQC5k6RKUg7nfKTyYguUmhoBvFdaOTuu+X6wo7IvmAfxr0unSu gYbb+8+aOaoS5KQF0XMNDg9u1cXzlwoueJJMljOcxfE8zXpzqfUCCIZRjuD6wQo098YiFtAnLsE 3HP4Mmffo7bF4/jMGlNhO+hKsXcT2WcrjDzwldn4Z/qFH0Rml6wZB0q3u8ADUx4AKOCFGvya4hs C6apKMt4XDsB4ajEFm0l2FNCZPEFIaA03mBn/ROyB+WG7udyBZq4T9ceoB3o= X-Received: by 2002:a05:690c:b9c:b0:8b3:5314:54ff with SMTP id 00721157ae682-8b660d480camr1188937b3.62.1796335251051; Thu, 03 Dec 2026 14:00:51 -0800 (PST) Received: from localhost ([2a03:2880:10ff:4b::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660df8d33sm977387b3.39.2026.12.03.14.00.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:49 -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 20/20] mm/swap: move virtual swap metadata into vswap clusters Date: Thu, 3 Dec 2026 14:00:10 -0800 Message-ID: <20261203220010.1302207-21-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: 1rgu6t4rjrio9ekf9gx9tykfporufc8t X-Rspam-User: 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-Rspamd-Queue-Id: 440A920006 X-Rspamd-Server: rspam01 X-HE-Tag: 1796335252-473591 X-HE-Meta: U2FsdGVkX198r7mZ0rQzPlhPyl6gbZkujqPkuIF4i4T+aZA1wVR4yJ8wUHV+xRxfXfpALWXCCrVbgcYwLITwdjeUgmVCA6hhsp6RiV3rKOXgNxF4zxn7NyEe5eNn6C6s10IjIAPvC5Vch2q3/ytsziLQQKviciIz4aZA3cE5r8n95wYkVDa4TuKNffOkot0j3+TfME2rL0zCuGmNOS/ZNxRh1rVez0SuriPbwAD4kC/e4UEO4v1tQiygaxax8zHSciWNlR8A2h7h+Q+xsrEXZTzWfYCojGwmdrSukVURwJ1VpCYiQn+XkCdYJRJnZxSjCBcI2yXNZIbt0PqzQ9pfMk9/KK3w2SpOvwFKuPZTeykRBOqYoKLc9Z7atKBJI1cVayTos17rUCovnhK98eg/swrGN8Y7qE+GQKH3URls3iCqct3xafGVlnDfneiCpO68RYJsq3TAiTxDIBgR5J3enGkRw0DruhagYY3an/rD6VTrj+pJCjlnSsOWYmyzvPV9OW1LX8ixQZ/HjkzQpa7RBqkkG1dhrDtK64d2TYWjQiulwU/S+EUhTVCexkGNLVa9y5ofInY5UglNYsfzqvQsR4uyp/xir1gzkmLjoxHtaR8NEWLOe/YuKnbzEnh+nf+Sxgswocq1p22PKdn/+MEFfCAQ+pNGSSAvXd2LoWmIEnchrSXZo1HtvhNr8e26omeLWCz8sA/AQJyTuOd82H55hM3q9G5e6PBEDasZDKT9vDOH/QNZxkLch0SNPJqmxMO069jXqMaZvqJ5yWkDGIVHaUWuLnCKpV0hfrg5Dm6SZ7aKyQkI5x9fhhFF2ydJPp6vHm4uPjno0vgp47sxmXOZ4usuT1IrAVQcHnq00Pnbk4q1s07cvyE+pdrulrb0xqMxKr7Bizn8Nw8VEQlxhpByJy6/7hpbDQZGdwK/er1WWOc5QeRd+msEOt4AVVgZA6lkb3ySnqWfxxpBjVGoPAj ZuFL4O0y 3jHkllNjEnJf+fdY/ej9SXObxbVMzVnOluKj7GkIpRB1SmexfJW3pgPm6axmHLg+OMLwbTQwVhxAQqrw/hrkb7N2oDeEeaM8KXaHDSg/AuvsTsLniUb39r8VVvA++tviJ+skwkLua0ptwbeFW9tVMSCwPsxu/Z/Y48B+vClQ8CcxiNj+m3gPmANIxuRKnylULG9ysxtpyUHMSv1tqSAEZRVnLi9qOwS/l/RvS7oSxHXIhbosfmZ0HNXJl2QCHvwPLxWc9o5UQJPuyNCpBPVd89OP3d4+jMGMgfIj30I30Fx0Joc1Zb0Yh2tQYnxUjRT4sCPtmhcKomaTV6/Dy8TVt4su8DLPZdoTCxmORV0gjQQKFRbddQPMrGBqjuokPMEa9IpZafyGsWopdLG0RXoja7m1ZwwfQBUoPaVB67xHefmHj674/i60BRukW4NlQMPmPa7ZUREJuo63EO1QjKr66LQaoYlIHpynXvJm00XzpNMkog3o+z/rQXx7QUZU1oUP2wdhnFwMRdqH8bfeI9B0uevwgYE2rByCswAPKytRFpOx3NPu7YYvVAFBPwK2zzYyuOOGKkFQmnPBaE1z9XhWKmbZ34MzhIOXubz+d9Bt/c9ObSYSlNDqHx9EEpyeDScxqQSiPLmN9Tv6eTiXX4YDjHeW7aDNzaag4UIPQ8dt6bk/iUitCC4nMGDuA88SJImWdS0JuahAsrS6ugVpG9G0YHoOaM+mk7MY1hwZnxENRjEg1nJ3uuRvCOc5cGQ== 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 Physical swap tables hold reverse mappings and marks for bad and raw hibernation slots. Ordinary swap uses the virtual slot for its cgroup owner, swap count and zero state. Hibernation accounts its slots to the root cgroup directly. Move the cgroup table, overflow count table and zero bitmap pointers into the vswap cluster descriptor. This saves 16 bytes per physical cluster on x86-64 with CONFIG_MEMCG without growing the virtual descriptor. Where the zero flag does not fit in the swap table, allocate the separate bitmap only for virtual clusters. Signed-off-by: Nhat Pham --- mm/memcontrol-v1.c | 4 +- mm/memcontrol.c | 6 +- mm/swap.h | 14 ++-- mm/swap_state.c | 6 +- mm/swap_table.h | 51 +++++++----- mm/swapfile.c | 194 ++++++++++++++++++++++++++------------------- mm/vswap.h | 6 +- 7 files changed, 161 insertions(+), 120 deletions(-) diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c index 570bb5006328..cd365873861f 100644 --- a/mm/memcontrol-v1.c +++ b/mm/memcontrol-v1.c @@ -313,7 +313,7 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci) if (nr_swapfile) mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile); - __swap_cgroup_set(ci, start, nr_entries, private_id); + __swap_cgroup_set(ci_dyn, start, nr_entries, private_id); folio_unqueue_deferred_split(folio); folio->memcg_data = 0; @@ -383,8 +383,8 @@ void memcg1_swapin(struct folio *folio) nr_pages = folio_nr_pages(folio); start = swp_cluster_offset(folio->swap); ci = swap_cluster_get_and_lock(folio); - id = __swap_cgroup_clear(ci, start, nr_pages); ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + id = __swap_cgroup_clear(ci_dyn, start, nr_pages); for (i = start; i < start + nr_pages; i++) if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE) nr_swapfile++; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 03f78a11138f..58478af5696b 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6093,7 +6093,7 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci) for (i = start; i < end; i++) if (__swp_tb_get_count(__swap_table_get(ci, i))) - __swap_cgroup_set(ci, i, 1, id); + __swap_cgroup_set(ci_dyn, i, 1, id); } #endif @@ -6185,10 +6185,12 @@ int __mem_cgroup_swap_charge(unsigned short id, unsigned int nr_pages) */ void __mem_cgroup_swap_record(struct folio *folio, unsigned short id) { + struct swap_cluster_info_dynamic *ci_dyn; struct swap_cluster_info *ci; ci = swap_cluster_get_and_lock(folio); - __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + __swap_cgroup_set(ci_dyn, swp_cluster_offset(folio->swap), folio_nr_pages(folio), id); swap_cluster_unlock(ci); } diff --git a/mm/swap.h b/mm/swap.h index 0d87de6eaddf..1bf4821cdfef 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -57,13 +57,6 @@ struct swap_cluster_info { u8 flags; u8 order; atomic_long_t __rcu *table; /* Swap table entries, see mm/swap_table.h */ - unsigned int *extend_table; /* For large swap count, protected by ci->lock */ -#ifdef CONFIG_MEMCG - struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */ -#endif -#if !SWAP_TABLE_HAS_ZEROFLAG - unsigned long *zero_bitmap; -#endif struct list_head list; }; @@ -72,6 +65,13 @@ struct swap_cluster_info_dynamic { unsigned int index; /* for cluster_index() */ struct rcu_head rcu; atomic_long_t *virtual_table; /* Backing pointers for vswap slots */ + unsigned int *extend_table; /* For large swap count, protected by ci->lock */ +#ifdef CONFIG_MEMCG + struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */ +#endif +#if !SWAP_TABLE_HAS_ZEROFLAG + unsigned long *zero_bitmap; +#endif }; /* All on-list cluster must have a non-zero flag. */ diff --git a/mm/swap_state.c b/mm/swap_state.c index b03871d63408..003147bd7ac4 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -201,10 +201,11 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci, */ if (!swp_tb_is_shadow(old_tb) || !__swp_tb_get_count(old_tb)) return -ENOENT; + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); if (shadowp) *shadowp = swp_tb_to_shadow(old_tb); if (memcg_id) - *memcg_id = __swap_cgroup_get(ci, ci_off); + *memcg_id = __swap_cgroup_get(ci_dyn, ci_off); if (nr == 1) return 0; @@ -214,7 +215,6 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci, * (e.g. uniformly ZSWAP, or mixed). The order-fallback loop in * __swap_cache_alloc_folio will retry with a smaller order on -EBUSY. */ - ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); ret = __vswap_check_backing(ci_dyn, round_down(ci_off, nr), nr, &type); if (ret != nr || type == VSWAP_ZSWAP) return -EBUSY; @@ -240,7 +240,7 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci, !swp_tb_is_shadow(old_tb) || !__swp_tb_get_count(old_tb) || is_zero != __swap_table_test_zero(ci, ci_off) || - (memcg_id && *memcg_id != __swap_cgroup_get(ci, ci_off)))) + (memcg_id && *memcg_id != __swap_cgroup_get(ci_dyn, ci_off)))) return -EBUSY; } while (++ci_off < ci_end); diff --git a/mm/swap_table.h b/mm/swap_table.h index f723ce4f2fca..bd1d9909113c 100644 --- a/mm/swap_table.h +++ b/mm/swap_table.h @@ -316,8 +316,11 @@ static inline void __swap_table_set_zero(struct swap_cluster_info *ci, swp_tb |= SWP_TB_ZERO_FLAG; __swap_table_set(ci, ci_off, swp_tb); #else + struct swap_cluster_info_dynamic *ci_dyn; + + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); lockdep_assert_held(&ci->lock); - __set_bit(ci_off, ci->zero_bitmap); + __set_bit(ci_off, ci_dyn->zero_bitmap); #endif } @@ -330,7 +333,10 @@ static inline bool __swap_table_test_zero(struct swap_cluster_info *ci, VM_WARN_ON(!swp_tb_is_countable(swp_tb)); return !!(swp_tb & SWP_TB_ZERO_FLAG); #else - return test_bit(ci_off, ci->zero_bitmap); + struct swap_cluster_info_dynamic *ci_dyn; + + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + return test_bit(ci_off, ci_dyn->zero_bitmap); #endif } @@ -344,62 +350,67 @@ static inline void __swap_table_clear_zero(struct swap_cluster_info *ci, swp_tb &= ~SWP_TB_ZERO_FLAG; __swap_table_set(ci, ci_off, swp_tb); #else + struct swap_cluster_info_dynamic *ci_dyn; + + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); lockdep_assert_held(&ci->lock); - __clear_bit(ci_off, ci->zero_bitmap); + __clear_bit(ci_off, ci_dyn->zero_bitmap); #endif } #ifdef CONFIG_MEMCG -static inline void __swap_cgroup_set(struct swap_cluster_info *ci, - unsigned int ci_off, unsigned long nr, unsigned short id) +static inline void __swap_cgroup_set(struct swap_cluster_info_dynamic *ci_dyn, + unsigned int ci_off, unsigned long nr, + unsigned short id) { - lockdep_assert_held(&ci->lock); + lockdep_assert_held(&ci_dyn->ci.lock); VM_WARN_ON_ONCE(ci_off >= SWAPFILE_CLUSTER); - if (WARN_ON_ONCE(!ci->memcg_table)) + if (WARN_ON_ONCE(!ci_dyn->memcg_table)) return; do { - ci->memcg_table->id[ci_off++] = id; + ci_dyn->memcg_table->id[ci_off++] = id; } while (--nr); } -static inline unsigned short __swap_cgroup_get(struct swap_cluster_info *ci, +static inline unsigned short __swap_cgroup_get(struct swap_cluster_info_dynamic *ci_dyn, unsigned int ci_off) { - lockdep_assert_held(&ci->lock); + lockdep_assert_held(&ci_dyn->ci.lock); VM_WARN_ON_ONCE(ci_off >= SWAPFILE_CLUSTER); - if (unlikely(!ci->memcg_table)) + if (unlikely(!ci_dyn->memcg_table)) return 0; - return ci->memcg_table->id[ci_off]; + return ci_dyn->memcg_table->id[ci_off]; } -static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info *ci, +static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info_dynamic *ci_dyn, unsigned int ci_off, unsigned long nr) { - unsigned short old = __swap_cgroup_get(ci, ci_off); + unsigned short old = __swap_cgroup_get(ci_dyn, ci_off); if (!old) return 0; do { - VM_WARN_ON_ONCE(ci->memcg_table->id[ci_off] != old); - ci->memcg_table->id[ci_off++] = 0; + VM_WARN_ON_ONCE(ci_dyn->memcg_table->id[ci_off] != old); + ci_dyn->memcg_table->id[ci_off++] = 0; } while (--nr); return old; } #else -static inline void __swap_cgroup_set(struct swap_cluster_info *ci, - unsigned int ci_off, unsigned long nr, unsigned short id) +static inline void __swap_cgroup_set(struct swap_cluster_info_dynamic *ci_dyn, + unsigned int ci_off, unsigned long nr, + unsigned short id) { } -static inline unsigned short __swap_cgroup_get(struct swap_cluster_info *ci, +static inline unsigned short __swap_cgroup_get(struct swap_cluster_info_dynamic *ci_dyn, unsigned int ci_off) { return 0; } -static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info *ci, +static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info_dynamic *ci_dyn, unsigned int ci_off, unsigned long nr) { diff --git a/mm/swapfile.c b/mm/swapfile.c index 3f6c56c51c2c..a9ee5bd1f86b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -443,19 +443,21 @@ static void swap_cluster_free_count_table(struct swap_table *table) swap_cluster_free_table_folio_rcu_cb); } -static void swap_cluster_free_table(struct swap_cluster_info *ci) +static void swap_cluster_free_table(struct swap_cluster_info *ci, + struct swap_cluster_info_dynamic *ci_dyn) { struct swap_table *table; + if (ci_dyn) { #ifdef CONFIG_MEMCG - kfree(ci->memcg_table); - ci->memcg_table = NULL; + kfree(ci_dyn->memcg_table); + ci_dyn->memcg_table = NULL; #endif - #if !SWAP_TABLE_HAS_ZEROFLAG - kfree(ci->zero_bitmap); - ci->zero_bitmap = NULL; + bitmap_free(ci_dyn->zero_bitmap); + ci_dyn->zero_bitmap = NULL; #endif + } table = (struct swap_table *)rcu_access_pointer(ci->table); if (!table) @@ -471,12 +473,12 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci) */ static int swap_cluster_alloc_table(struct swap_info_struct *si, struct swap_cluster_info *ci, + struct swap_cluster_info_dynamic *ci_dyn, 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; @@ -494,19 +496,20 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si, if (!table) goto lock; + if (ci_dyn) { #ifdef CONFIG_MEMCG - if (vswap && !mem_cgroup_disabled()) { - memcg_table = kzalloc_obj(*ci->memcg_table, gfp); - if (!memcg_table) - goto lock; - } + if (!mem_cgroup_disabled()) { + memcg_table = kzalloc_obj(*memcg_table, gfp); + if (!memcg_table) + goto lock; + } #endif - #if !SWAP_TABLE_HAS_ZEROFLAG - zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp); - if (!zero_bitmap) - goto lock; + zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp); + if (!zero_bitmap) + goto lock; #endif + } ret = 0; lock: @@ -528,14 +531,16 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si, /* The cluster must be empty and not on any list during allocation. */ VM_WARN_ON_ONCE(!cluster_is_empty(ci)); + if (ci_dyn) { #ifdef CONFIG_MEMCG - VM_WARN_ON_ONCE(ci->memcg_table); - ci->memcg_table = memcg_table; + VM_WARN_ON_ONCE(ci_dyn->memcg_table); + ci_dyn->memcg_table = memcg_table; #endif #if !SWAP_TABLE_HAS_ZEROFLAG - VM_WARN_ON_ONCE(ci->zero_bitmap); - ci->zero_bitmap = zero_bitmap; + VM_WARN_ON_ONCE(ci_dyn->zero_bitmap); + ci_dyn->zero_bitmap = zero_bitmap; #endif + } /* * Make tables visible to cluster_is_usable() after everything is @@ -562,6 +567,7 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si, * bad slots for swapoff, and non-swapoff path must never free bad slots. */ static void swap_cluster_assert_empty(struct swap_cluster_info *ci, + struct swap_cluster_info_dynamic *ci_dyn, unsigned int ci_off, unsigned int nr, bool swapoff) { @@ -578,11 +584,12 @@ static void swap_cluster_assert_empty(struct swap_cluster_info *ci, bad_slots++; else WARN_ON_ONCE(!swp_tb_is_null(swp_tb)); - WARN_ON_ONCE(__swap_cgroup_get(ci, ci_off)); + if (ci_dyn) + WARN_ON_ONCE(__swap_cgroup_get(ci_dyn, ci_off)); } while (++ci_off < ci_end); WARN_ON_ONCE(bad_slots != (swapoff ? ci->count : 0)); - WARN_ON_ONCE(nr == SWAPFILE_CLUSTER && ci->extend_table); + WARN_ON_ONCE(nr == SWAPFILE_CLUSTER && ci_dyn && ci_dyn->extend_table); } /* @@ -606,7 +613,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, NULL, __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN, true)) return ci; @@ -620,7 +627,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, NULL, __GFP_HIGH | __GFP_NOMEMALLOC | GFP_KERNEL, false); /* @@ -674,14 +681,15 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si, static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci) { - swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, false); - swap_cluster_free_table(ci); + struct swap_cluster_info_dynamic *ci_dyn = NULL; - if (swap_is_vswap(si)) { - struct swap_cluster_info_dynamic *ci_dyn; + if (swap_is_vswap(si)) + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + swap_cluster_assert_empty(ci, ci_dyn, 0, SWAPFILE_CLUSTER, false); + swap_cluster_free_table(ci, ci_dyn); + if (ci_dyn) { /* vswap clusters are destroyed, not returned to free_clusters. */ - ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); xa_erase(&si->cluster_info_pool, ci_dyn->index); move_cluster(si, ci, NULL, CLUSTER_FLAG_DEAD); vswap_cluster_free_vtable(ci); @@ -910,7 +918,11 @@ 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); + struct swap_cluster_info_dynamic *ci_dyn = NULL; + + if (alloc_si) + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + ret = swap_cluster_alloc_table(alloc_si, ci, ci_dyn, GFP_KERNEL, false); if (ret) goto out; } else { @@ -1088,10 +1100,14 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si, * The slot carries no swap count and is freed by offset. */ if (likely(folio)) { + struct swap_cluster_info_dynamic *ci_dyn = NULL; + order = folio_order(folio); nr_pages = 1 << order; - swap_cluster_assert_empty(ci, ci_off, nr_pages, false); - if (!swap_is_vswap(si)) { + if (swap_is_vswap(si)) + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + swap_cluster_assert_empty(ci, ci_dyn, ci_off, nr_pages, false); + if (!ci_dyn) { /* * Folio already in the swap cache: we are allocating * physical backing for its vswap entry. Point each @@ -1115,7 +1131,7 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si, } else if (IS_ENABLED(CONFIG_HIBERNATION)) { order = 0; nr_pages = 1; - swap_cluster_assert_empty(ci, ci_off, 1, false); + swap_cluster_assert_empty(ci, NULL, ci_off, 1, false); /* Exclusively owned by hibernation, must never enter the swap cache */ __swap_table_set(ci, ci_off, SWP_TB_HIB); } else { @@ -1249,7 +1265,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, ci_dyn, GFP_ATOMIC | __GFP_NOWARN, true)) { spin_unlock(&ci->lock); kfree(ci_dyn); return SWAP_ENTRY_INVALID; @@ -1257,7 +1273,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si, if (vswap_cluster_alloc_vtable(ci_dyn, GFP_ATOMIC | __GFP_NOWARN)) { spin_unlock(&ci->lock); - swap_cluster_free_table(ci); + swap_cluster_free_table(ci, ci_dyn); kfree(ci_dyn); return SWAP_ENTRY_INVALID; } @@ -1266,7 +1282,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si, XA_LIMIT(1, DIV_ROUND_UP(si->max, SWAPFILE_CLUSTER) - 1), GFP_ATOMIC | __GFP_NOWARN)) { spin_unlock(&ci->lock); - swap_cluster_free_table(&ci_dyn->ci); + swap_cluster_free_table(ci, ci_dyn); vswap_cluster_free_vtable(&ci_dyn->ci); kfree(ci_dyn); return SWAP_ENTRY_INVALID; @@ -1718,14 +1734,14 @@ static bool swap_sync_discard(void) return false; } -static int swap_extend_table_alloc(struct swap_info_struct *si, - struct swap_cluster_info *ci, +static int swap_extend_table_alloc(struct swap_cluster_info_dynamic *ci_dyn, unsigned int ci_off, gfp_t gfp) { + struct swap_cluster_info *ci = &ci_dyn->ci; int count; void *table; - table = kzalloc(sizeof(ci->extend_table[0]) * SWAPFILE_CLUSTER, gfp); + table = kzalloc(sizeof(ci_dyn->extend_table[0]) * SWAPFILE_CLUSTER, gfp); if (!table) return -ENOMEM; @@ -1741,10 +1757,10 @@ static int swap_extend_table_alloc(struct swap_info_struct *si, count = swp_tb_get_count(__swap_table_get(ci, ci_off)); if (count < (SWP_TB_COUNT_MAX - 1)) goto out_free; - if (ci->extend_table) + if (ci_dyn->extend_table) goto out_free; - ci->extend_table = table; + ci_dyn->extend_table = table; spin_unlock(&ci->lock); return 0; @@ -1759,6 +1775,7 @@ int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp) int ret; struct swap_info_struct *si; struct swap_cluster_info *ci; + struct swap_cluster_info_dynamic *ci_dyn; unsigned long offset = swp_offset(entry); si = get_swap_device(entry); @@ -1767,32 +1784,34 @@ int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp) /* The source PTE pins the entry, so its cluster is alive. */ ci = __swap_offset_to_cluster(si, offset); - ret = swap_extend_table_alloc(si, ci, swp_cluster_offset(entry), gfp); + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + ret = swap_extend_table_alloc(ci_dyn, swp_cluster_offset(entry), gfp); put_swap_device(si); return ret; } -static void swap_extend_table_try_free(struct swap_cluster_info *ci) +static void swap_extend_table_try_free(struct swap_cluster_info_dynamic *ci_dyn) { unsigned long i; - if (!ci->extend_table) + if (!ci_dyn->extend_table) return; for (i = 0; i < SWAPFILE_CLUSTER; i++) { - if (ci->extend_table[i]) + if (ci_dyn->extend_table[i]) return; } - kfree(ci->extend_table); - ci->extend_table = NULL; + kfree(ci_dyn->extend_table); + ci_dyn->extend_table = NULL; } /* Decrease the swap count of one slot, without freeing it */ -static void __swap_cluster_put_entry(struct swap_cluster_info *ci, - unsigned int ci_off) +static void __swap_cluster_put_entry(struct swap_cluster_info_dynamic *ci_dyn, + unsigned int ci_off) { + struct swap_cluster_info *ci = &ci_dyn->ci; int count; unsigned long swp_tb; @@ -1804,15 +1823,15 @@ static void __swap_cluster_put_entry(struct swap_cluster_info *ci, VM_WARN_ON_ONCE(count > SWP_TB_COUNT_MAX); if (count == SWP_TB_COUNT_MAX) { - count = ci->extend_table[ci_off]; + count = ci_dyn->extend_table[ci_off]; /* Overflow starts with SWP_TB_COUNT_MAX */ VM_WARN_ON_ONCE(count < SWP_TB_COUNT_MAX); count--; if (count == (SWP_TB_COUNT_MAX - 1)) { - ci->extend_table[ci_off] = 0; + ci_dyn->extend_table[ci_off] = 0; __swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, count)); } else { - ci->extend_table[ci_off] = count; + ci_dyn->extend_table[ci_off] = count; } } else { __swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, --count)); @@ -1825,7 +1844,7 @@ static void __swap_cluster_put_entry(struct swap_cluster_info *ci, * also possible that a pending dup just attached an extend table. */ if (unlikely(count == SWP_TB_COUNT_MAX - 2 || count == SWP_TB_COUNT_MAX - 1)) - swap_extend_table_try_free(ci); + swap_extend_table_try_free(ci_dyn); } /** @@ -1845,6 +1864,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si, pgoff_t offset, int nr, bool reclaim_cache) { + struct swap_cluster_info_dynamic *ci_dyn; struct swap_cluster_info *ci; unsigned int ci_off, ci_end; pgoff_t end = offset + nr; @@ -1854,6 +1874,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si, int ci_batch = -1; ci = swap_cluster_lock(si, offset); + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); ci_off = offset % SWAPFILE_CLUSTER; ci_end = ci_off + nr; do { @@ -1875,7 +1896,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si, * slots will be freed when folio is removed from swap cache * (__swap_cache_del_folio). */ - __swap_cluster_put_entry(ci, ci_off); + __swap_cluster_put_entry(ci_dyn, ci_off); if (ci_batch != -1) { __swap_cluster_free_entries(si, ci, ci_batch, ci_off - ci_batch); ci_batch = -1; @@ -1898,16 +1919,17 @@ static void swap_put_entries_cluster(struct swap_info_struct *si, } /* Increase the swap count of one slot. */ -static int __swap_cluster_dup_entry(struct swap_cluster_info *ci, +static int __swap_cluster_dup_entry(struct swap_cluster_info_dynamic *ci_dyn, unsigned int ci_off) { + struct swap_cluster_info *ci = &ci_dyn->ci; int count; unsigned long swp_tb; lockdep_assert_held(&ci->lock); swp_tb = __swap_table_get(ci, ci_off); /* Bad or special slots can't be handled */ - if (WARN_ON_ONCE(swp_tb_is_bad(swp_tb))) + if (WARN_ON_ONCE(!swp_tb_is_countable(swp_tb))) return -EINVAL; count = __swp_tb_get_count(swp_tb); /* Must be either cached or have a count already */ @@ -1916,19 +1938,19 @@ static int __swap_cluster_dup_entry(struct swap_cluster_info *ci, if (likely(count < (SWP_TB_COUNT_MAX - 1))) { __swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, count + 1)); - VM_WARN_ON_ONCE(ci->extend_table && ci->extend_table[ci_off]); + VM_WARN_ON_ONCE(ci_dyn->extend_table && ci_dyn->extend_table[ci_off]); } else if (count == (SWP_TB_COUNT_MAX - 1)) { - if (ci->extend_table) { - VM_WARN_ON_ONCE(ci->extend_table[ci_off]); - ci->extend_table[ci_off] = SWP_TB_COUNT_MAX; + if (ci_dyn->extend_table) { + VM_WARN_ON_ONCE(ci_dyn->extend_table[ci_off]); + ci_dyn->extend_table[ci_off] = SWP_TB_COUNT_MAX; __swap_table_set(ci, ci_off, __swp_tb_mk_count(swp_tb, SWP_TB_COUNT_MAX)); } else { return -ENOMEM; } } else if (count == SWP_TB_COUNT_MAX) { - VM_WARN_ON_ONCE(ci->extend_table[ci_off] >= - type_max(typeof(ci->extend_table[0]))); - ++ci->extend_table[ci_off]; + VM_WARN_ON_ONCE(ci_dyn->extend_table[ci_off] >= + type_max(typeof(ci_dyn->extend_table[0]))); + ++ci_dyn->extend_table[ci_off]; } else { /* Never happens unless counting went wrong */ WARN_ON_ONCE(1); @@ -1952,6 +1974,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si, pgoff_t offset, int nr) { int err; + struct swap_cluster_info_dynamic *ci_dyn; struct swap_cluster_info *ci; unsigned int ci_start, ci_off, ci_end; @@ -1959,13 +1982,14 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si, ci_end = ci_start + nr; ci_off = ci_start; ci = swap_cluster_lock(si, offset); + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); restart: do { - err = __swap_cluster_dup_entry(ci, ci_off); + err = __swap_cluster_dup_entry(ci_dyn, ci_off); if (unlikely(err)) { if (err == -ENOMEM) { spin_unlock(&ci->lock); - err = swap_extend_table_alloc(si, ci, ci_off, GFP_ATOMIC); + err = swap_extend_table_alloc(ci_dyn, ci_off, GFP_ATOMIC); spin_lock(&ci->lock); if (!err) goto restart; @@ -1978,7 +2002,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si, return 0; failed: while (ci_off-- > ci_start) - __swap_cluster_put_entry(ci, ci_off); + __swap_cluster_put_entry(ci_dyn, ci_off); swap_cluster_unlock(ci); return err; } @@ -2216,11 +2240,11 @@ void __vswap_release_backing(struct swap_cluster_info *ci, lockdep_assert_held(&ci->lock); ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); - batch_id = __swap_cgroup_get(ci, ci_start); + batch_id = __swap_cgroup_get(ci_dyn, ci_start); for (ci_off = ci_start; ci_off < ci_start + nr; ci_off++) { vt = __vtable_get(ci_dyn, ci_off); - cur_id = __swap_cgroup_get(ci, ci_off); + cur_id = __swap_cgroup_get(ci_dyn, ci_off); if (cur_id != batch_id) { mem_cgroup_swap_uncharge(batch_id, batch_nr, @@ -2391,7 +2415,7 @@ swp_entry_t folio_realloc_swap(struct folio *folio) spin_unlock(&ci->lock); return vtable_to_phys(vt); } - memcg_id = __swap_cgroup_get(ci, voff); + memcg_id = __swap_cgroup_get(ci_dyn, voff); spin_unlock(&ci->lock); phys_entry = folio_alloc_phys_swap(folio); @@ -2558,12 +2582,13 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry) */ static void __swap_cluster_finish_free(struct swap_info_struct *si, struct swap_cluster_info *ci, + struct swap_cluster_info_dynamic *ci_dyn, unsigned int ci_start, unsigned int nr_pages) { lockdep_assert_held(&ci->lock); swap_range_free(si, cluster_offset(si, ci) + ci_start, nr_pages); - swap_cluster_assert_empty(ci, ci_start, nr_pages, false); + swap_cluster_assert_empty(ci, ci_dyn, ci_start, nr_pages, false); if (!ci->count) free_cluster(si, ci); @@ -2582,12 +2607,9 @@ static void __swap_cluster_free_phys_slots(struct swap_info_struct *si, lockdep_assert_held(&ci->lock); VM_WARN_ON(ci->count < nr_pages); ci->count -= nr_pages; - for (ci_off = ci_start; ci_off < ci_start + nr_pages; ci_off++) { + for (ci_off = ci_start; ci_off < ci_start + nr_pages; ci_off++) __swap_table_set(ci, ci_off, null_to_swp_tb()); - if (!SWAP_TABLE_HAS_ZEROFLAG) - __swap_table_clear_zero(ci, ci_off); - } - __swap_cluster_finish_free(si, ci, ci_start, nr_pages); + __swap_cluster_finish_free(si, ci, NULL, ci_start, nr_pages); } /* @@ -2611,6 +2633,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si, struct swap_cluster_info *ci, unsigned int ci_start, unsigned int nr_pages) { + struct swap_cluster_info_dynamic *ci_dyn; unsigned long old_tb; unsigned short batch_id = 0, id_cur; unsigned int ci_off = ci_start, ci_end = ci_start + nr_pages; @@ -2618,6 +2641,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si, VM_WARN_ON(ci->count < nr_pages); + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); __vswap_release_backing(ci, ci_start, nr_pages); ci->count -= nr_pages; @@ -2640,7 +2664,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si, * backing of vswap entries was already uncharged by * __vswap_release_backing(). */ - id_cur = __swap_cgroup_clear(ci, ci_off, 1); + id_cur = __swap_cgroup_clear(ci_dyn, ci_off, 1); if (batch_id != id_cur) { if (batch_id) mem_cgroup_swap_put(batch_id, @@ -2653,7 +2677,7 @@ void __swap_cluster_free_entries(struct swap_info_struct *si, if (batch_id) mem_cgroup_swap_put(batch_id, ci_off - batch_off); - __swap_cluster_finish_free(si, ci, ci_start, nr_pages); + __swap_cluster_finish_free(si, ci, ci_dyn, ci_start, nr_pages); } int __swap_count(swp_entry_t entry) @@ -2700,8 +2724,12 @@ int swp_swapcount(swp_entry_t entry) ci = swap_cluster_lock(si, swp_offset(entry)); swp_tb = __swap_table_get(ci, swp_cluster_offset(entry)); count = swp_tb_get_count(swp_tb); - if (count == SWP_TB_COUNT_MAX) - count = ci->extend_table[swp_cluster_offset(entry)]; + if (count == SWP_TB_COUNT_MAX) { + struct swap_cluster_info_dynamic *ci_dyn; + + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + count = ci_dyn->extend_table[swp_cluster_offset(entry)]; + } swap_cluster_unlock(ci); put_swap_device(si); @@ -3879,9 +3907,9 @@ static void free_swap_cluster_info(struct swap_info_struct *si, ci = &ci_dyn->ci; spin_lock(&ci->lock); if (cluster_table_is_alloced(ci)) { - swap_cluster_assert_empty(ci, 0, + swap_cluster_assert_empty(ci, ci_dyn, 0, SWAPFILE_CLUSTER, true); - swap_cluster_free_table(ci); + swap_cluster_free_table(ci, ci_dyn); } spin_unlock(&ci->lock); vswap_cluster_free_vtable(ci); @@ -3898,8 +3926,8 @@ static void free_swap_cluster_info(struct swap_info_struct *si, /* Cluster with bad marks count will have a remaining table */ spin_lock(&ci->lock); if (cluster_table_is_alloced(ci)) { - swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, true); - swap_cluster_free_table(ci); + swap_cluster_assert_empty(ci, NULL, 0, SWAPFILE_CLUSTER, true); + swap_cluster_free_table(ci, NULL); } spin_unlock(&ci->lock); } diff --git a/mm/vswap.h b/mm/vswap.h index f3a55414c6b7..3af7d73b7b1c 100644 --- a/mm/vswap.h +++ b/mm/vswap.h @@ -107,9 +107,9 @@ static inline void swap_rmap_clear_cache_only(struct swap_cluster_info *ci, * reads atomic; it does not permit backend changes. Revalidate speculative * reads under the folio lock before using them. * - * Zero-backed slots use the swap_table per-slot zero flag (same as - * direct-mapped physical swap), via __swap_table_test_zero() and friends, - * which fall back to ci->zero_bitmap where the flag does not fit. Cached + * Zero-backed slots use the swap_table per-slot zero flag via + * __swap_table_test_zero() and friends, which fall back to + * ci_dyn->zero_bitmap where the flag does not fit. Cached * folios are read out of the swap_table PFN entry; there is no separate FOLIO * vtable type because the folio pointer would duplicate that PFN and * would go stale on folio migration / split. -- 2.53.1-Meta