mm, swap: Enable THP SWAP for PowerPC Book3S64
Subsystems (3)
Ritesh Harjani (IBM)
reviewerv3 cover mm, swap: Enable THP SWAP for PowerPC Book3S64
On PowerPC Book3S64, MMU is selected at runtime, so macros like PMD_SHIFT are effectively runtime variables in the Book3S64 code. THP swap code uses these macros for e.g. to size some of its array data structures based on PMD_ORDER. This patch series makes that usage dependent on the runtime variable and provides an upper-bound architecture override for cases (e.g. SWAP_NR_ORDERS), where the runtime conversion is not considered beneficial.
This series increases bandwidth throughput with zram backend for swapout by around 40-50% with Radix and 100-130% with Hash (Tested by Sayali)
Note that this patch series is based out of linux-next (next-20260608).
v2 -> v3:
=========
1. Fixed sparse warning for swap_table_use_page reported by lkp in patch-1
RFC -> v2:
==========
1. Send the unused leftovers change in swap.h separately [1]
2. Changed RFC Patch-3 design from runtime SWAP_NR_ORDERS to arch override (ARCH_MAX_PMD_ORDER) - suggested by Youngjun
3. Dropped RFC tag
[1]: https://lore.kernel.org/linux-mm/68591daf0d679e5a0072d63751f187d14613e2b0.1781146877.git.ritesh.list@gmail.com/
[RFC]: https://lore.kernel.org/linux-mm/[email protected]/
[v2]: https://lore.kernel.org/linuxppc-dev/[email protected]/
Ritesh Harjani (IBM) (3):
mm, swap: make SWAPFILE_CLUSTER runtime
mm, swap: allow archs to override SWAP_NR_ORDERS via ARCH_MAX_PMD_ORDER
powerpc: Kconfig: Enable THP_SWAP on Book3S64
arch/powerpc/include/asm/book3s/64/pgtable.h | 7 +++++ arch/powerpc/platforms/Kconfig.cputype | 1 + include/linux/swap.h | 12 ++++++++- mm/swap.h | 5 ++-- mm/swap_table.h | 6 ++--- mm/swapfile.c | 27 ++++++++++++++++---- 6 files changed, 46 insertions(+), 12 deletions(-)
--
2.39.5
Ritesh Harjani (IBM)
reviewerPATCH v3 1/3 mm, swap: make SWAPFILE_CLUSTER runtime
On PowerPC Book3S64, MMU is selected at runtime, so macros like PMD_SHIFT are effectively runtime variables in the Book3S64 code. THP swap code uses these macros to size some of its array data structures based on PMD_ORDER e.g. SWAPFILE_CLUSTER macro is used for this very purpose.
Hence this patch initializes SWAPFILE_CLUSTER at runtime and also modifies swap_table and swap_memcg_table which were earlier using this macro for defining the number of table entries.
--- mm/swap.h | 5 +++-- mm/swap_table.h | 6 ++---- mm/swapfile.c | 27 ++++++++++++++++++++++----- 3 files changed, 27 insertions(+), 11 deletions(-)
mm/swap.h +3 -2
@@ -26,11 +26,12 @@ extern int page_cluster;#define SWAP_TABLE_HAS_ZEROFLAG ((BITS_PER_LONG - SWAP_CACHE_PFN_MARK_BITS - \ SWAP_CACHE_PFN_BITS) > SWAP_COUNT_MIN_BITS) +extern unsigned int swap_slots_in_cluster__read_mostly ; +#define SWAPFILE_CLUSTER swap_slots_in_cluster +#ifdef CONFIG_THP_SWAP -#define SWAPFILE_CLUSTER HPAGE_PMD_NR#define swap_entry_order (order) (order)#else -#define SWAPFILE_CLUSTER 256#define swap_entry_order (order) 0#endif
mm/swap_table.h +2 -4
@@ -8,16 +8,14 @@/* A typical flat array in each cluster as swap table */ struct swap_table { -atomic_long_t entries[SWAPFILE_CLUSTER ]; + DECLARE_FLEX_ARRAY(atomic_long_t, entries); };/* For storing memcg private id */ struct swap_memcg_table { -unsigned short id[SWAPFILE_CLUSTER ]; + DECLARE_FLEX_ARRAY(unsigned short, id); }; -#define SWP_TABLE_USE_PAGE (sizeof(struct swap_table) == PAGE_SIZE) -/* * A swap table entry represents the status of a swap slot on a swap * (physical or virtual) device. The swap table in each cluster is a
mm/swapfile.c +22 -5
@@ -129,6 +129,17 @@ static DEFINE_PER_CPU(struct percpu_swap_cluster, percpu_swap_cluster) = { .lock = INIT_LOCAL_LOCK(), }; +unsigned int swap_slots_in_cluster__read_mostly ; +static bool swap_table_use_page__read_mostly ; + +static unsigned int generic_swap_slots_in_clusters (void ) +{ +if (IS_ENABLED (CONFIG_THP_SWAP )) +return HPAGE_PMD_NR ; +else +return 256 ; +} +/* May return NULL on invalid type, caller must check for NULL return */ static struct swap_info_struct *swap_type_to_info (int type) {
@@ -437,7 +448,7 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci)return ;rcu_assign_pointer (ci->table,NULL ); -if (!SWP_TABLE_USE_PAGE ) { +if (!swap_table_use_page) {kmem_cache_free (swap_table_cachep, table);return ; }
@@ -456,7 +467,7 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)if (rcu_access_pointer (ci->table))return 0 ; -if (SWP_TABLE_USE_PAGE ) { +if (swap_table_use_page) { folio =folio_alloc (gfp | __GFP_ZERO,0 );if (folio) table =folio_address (folio);
@@ -471,7 +482,8 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)#ifdef CONFIG_MEMCG if (!mem_cgroup_disabled ()) {VM_WARN_ON_ONCE (ci->memcg_table); - ci->memcg_table =kzalloc_obj (*ci->memcg_table, gfp); + ci->memcg_table =kzalloc_flex (*ci->memcg_table, id, +SWAPFILE_CLUSTER , gfp);if (!ci->memcg_table) {swap_cluster_free_table (ci);return -ENOMEM ;
@@ -3912,14 +3924,19 @@ static int __init swapfile_init(void) { swapfile_maximum_size =arch_max_swapfile_size (); + swap_slots_in_cluster =generic_swap_slots_in_clusters (); + swap_table_use_page = + (swap_slots_in_cluster *sizeof (atomic_long_t) ==PAGE_SIZE ); +/* * Once a cluster is freed, it's swap table content is read * only, and all swap cache readers (swap_cache_*) verifies * the content before use. So it's safe to use RCU slab here. */ -if (!SWP_TABLE_USE_PAGE ) +if (!swap_table_use_page) swap_table_cachep =kmem_cache_create ("swap_table" , -sizeof (struct swap_table ), + struct_size_t(struct swap_table, entries, +SWAPFILE_CLUSTER ),0 ,SLAB_PANIC |SLAB_TYPESAFE_BY_RCU ,NULL );#ifdef CONFIG_MIGRATION
--
2.39.5
Re: [PATCH v3 1/3] mm, swap: make SWAPFILE_CLUSTER runtime
Quoted 9 lines · Ritesh Harjani (IBM)
On PowerPC Book3S64, MMU is selected at runtime, so macros like
PMD_SHIFTare effectively runtime variables in the Book3S64 code. THP swap code uses these macros to size some of its array data structures based onPMD_ORDERe.g.SWAPFILE_CLUSTERmacro is used for this very purpose.
Hence this patch initializesSWAPFILE_CLUSTERat runtime and also modifiesswap_tableandswap_memcg_tablewhich were earlier using this macro for defining the number of table entries.
Quoted 6 lines
Signed-off-by Ritesh Harjani (IBM) <[email protected]>--- mm/swap.h | 5 +++-- mm/swap_table.h | 6 ++---- mm/swapfile.c | 27 ++++++++++++++++++++++----- 3 files changed, 27 insertions(+), 11 deletions(-)
Hi Ritesh,
Thanks for the patch.
mm/swap.h extern int page_cluster; · line 29
Quoted 10 lines
mm/swap.h +1
-0@@ -26,11 +26,12 @@ extern int page_cluster;#define SWAP_TABLE_HAS_ZEROFLAG ((BITS_PER_LONG - SWAP_CACHE_PFN_MARK_BITS - \ SWAP_CACHE_PFN_BITS) > SWAP_COUNT_MIN_BITS) +extern unsigned int swap_slots_in_cluster__read_mostly ;
Maybe __ro_after_init is better for this kind of use case?
mm/swap.h extern int page_cluster; · line 30
Quoted 4 lines
mm/swap.h +2
-1+#define SWAPFILE_CLUSTER swap_slots_in_cluster +#ifdef CONFIG_THP_SWAP -#define SWAPFILE_CLUSTER HPAGE_PMD_NR
So on Book3S64, HPAGE_PMD_NR is also a variable right? Then we don't really need to change the SWAPFILE_CLUSTER defination here? We just need to adjust the users of this macro so the build will pass?
Or maybe use another macro instead of HPAGE_PMD_NR here, whichever is more arch friendly.
That way if that is a build time constant, all users are folded to just one mask/shift which is super efficient especially for inline helpers, and there are a lot of them. Only special archs live with the dynamic load overhead.
Ritesh Harjani (IBM)
reviewerRe: [PATCH v3 1/3] mm, swap: make SWAPFILE_CLUSTER runtime
Quoted 17 lines · Kairui Song
Quoted 9 lines · Ritesh Harjani (IBM)
On PowerPC Book3S64, MMU is selected at runtime, so macros like
PMD_SHIFTare effectively runtime variables in the Book3S64 code. THP swap code uses these macros to size some of its array data structures based onPMD_ORDERe.g.SWAPFILE_CLUSTERmacro is used for this very purpose.
Hence this patch initializesSWAPFILE_CLUSTERat runtime and also modifiesswap_tableandswap_memcg_tablewhich were earlier using this macro for defining the number of table entries.Quoted 6 lines
Signed-off-by Ritesh Harjani (IBM) <[email protected]>--- mm/swap.h | 5 +++-- mm/swap_table.h | 6 ++---- mm/swapfile.c | 27 ++++++++++++++++++++++----- 3 files changed, 27 insertions(+), 11 deletions(-)
Quoted 4 lines
Hi Ritesh,
Thanks for the patch.
Thanks Kairui for taking a look at this. Sorry about the delayed response, (was on travel to #OSSMumbai).
Quoted 9 lines
Quoted 9 lines
mm/swap.h +0
-0@@ -26,11 +26,12 @@ extern int page_cluster;#define SWAP_TABLE_HAS_ZEROFLAG ((BITS_PER_LONG - SWAP_CACHE_PFN_MARK_BITS - \ SWAP_CACHE_PFN_BITS) > SWAP_COUNT_MIN_BITS)
Quoted 4 lines
mm/swap.h extern int page_cluster; · line 29
Quoted 1 line
mm/swap.h +1
-0+extern unsigned int swap_slots_in_cluster__read_mostly ;Maybe
__ro_after_initis better for this kind of use case?
yup. I can change that.
Quoted 9 lines
mm/swap.h extern int page_cluster; · line 30
Quoted 4 lines
mm/swap.h +2
-1+#define SWAPFILE_CLUSTER swap_slots_in_cluster +#ifdef CONFIG_THP_SWAP -#define SWAPFILE_CLUSTER HPAGE_PMD_NRSo on Book3S64,
HPAGE_PMD_NRis also a variable right? Then we don't really need to change theSWAPFILE_CLUSTERdefination here? We just need to adjust the users of this macro so the build will pass?
Right. Earlier I was playing with different values of this via an arch_xxx variant. But for book3s64 usecase, the default values are fine.
So, I agree, we need not change SWAPFILE_CLUSTER here. I can update the patch with the suggested changes in v4.
-ritesh
Ritesh Harjani (IBM)
reviewerPATCH v3 2/3 mm, swap: allow archs to override SWAP_NR_ORDERS via ARCH_MAX_PMD_ORDER
In reply to Ritesh Harjani (IBM)SWAP_NR_ORDERS sizes a few small bounded arrays inside THP swap allocator code (nofull/frag cluster lists, percpu_swap_cluster's si/offset arrays, next array for rotational device). This currently expands to PMD_ORDER+1, which only works when PMD_ORDER is a compile time constant.
However on architecture like PowerPC Book3S64, PMD_ORDER is a runtime variable which depends upon which MMU is selected (Radix / Hash), so in that case, PMD_ORDER cannot be used to size the static arrays.
This patch provides an optional ARCH_MAX_PMD_ORDER (upper-bound) override for such architectures. The memory overhead on enabling this override is negligible. Even if we make SWAP_NR_ORDERS runtime alloc, default slab padding could cause some memory waste. Also we lose the per-cpu cacheline benefits (for percpu_swap_cluster) because it might cost an extra cacheline indirection overhead in swap_alloc_fast() for fetching si[order]/offset[order]. Note that a fully runtime SWAP_NR_ORDERS was considered in previous version but was dropped for this reason [1]
[1]: https://lore.kernel.org/linuxppc-dev/[email protected]/
--- arch/powerpc/include/asm/book3s/64/pgtable.h | 7 +++++++ include/linux/swap.h | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/book3s/64/pgtable.h +7 -0
@@ -204,6 +204,13 @@ extern unsigned long __pmd_frag_size_shift;#define MAX_PTRS_PER_PGD (1 << (H_PGD_INDEX_SIZE > RADIX_PGD_INDEX_SIZE ? \ H_PGD_INDEX_SIZE : RADIX_PGD_INDEX_SIZE)) +/* +* Compile-time upper bound on PMD_ORDER across hash and radix MMUs. +* Used by THP SWAP code. Check include/linux/swap.h +*/ +#define ARCH_MAX_PMD_ORDER ((H_PTE_INDEX_SIZE > RADIX_PTE_INDEX_SIZE) ? \ + H_PTE_INDEX_SIZE : RADIX_PTE_INDEX_SIZE) +/* PMD_SHIFT determines what a second-level page table entry can map */ #define PMD_SHIFT (PAGE_SHIFT + PTE_INDEX_SIZE)#define PMD_SIZE (1UL << PMD_SHIFT)
include/linux/swap.h +11 -1
@@ -229,11 +229,21 @@ enum {*/ #define SWAP_ENTRY_INVALID 0 +/* +* ARCH_MAX_PMD_ORDER is an optional arch hook: a compile-time upper bound for +* PMD_ORDER across all possible MMU configurations of that arch. It is used to +* size SWAP_NR_ORDERS on architectures (e.g. powerpc book3s64) where PMD_ORDER +* is selected at boot rather than at compile time. +*/ #ifdef CONFIG_THP_SWAP +#ifdef ARCH_MAX_PMD_ORDER +#define SWAP_NR_ORDERS (ARCH_MAX_PMD_ORDER + 1) +#else #define SWAP_NR_ORDERS (PMD_ORDER + 1) +#endif /* ARCH_MAX_PMD_ORDER */ #else #define SWAP_NR_ORDERS 1 -#endif +#endif /* CONFIG_THP_SWAP */ /* * We keep using same cluster for rotational device so IO will be sequential.
Signature · 1 line
2.39.5
Ritesh Harjani (IBM)
reviewerPATCH v3 3/3 powerpc: Kconfig: Enable THP_SWAP on Book3S64
In reply to Ritesh Harjani (IBM)THP_SWAP avoids splitting of a transparent huge folio into 32 smaller 64K folios (Radix-64K pagesize / 2M PMD) or into 256 smaller 64K folios (Hash-64K pagesize / 16M PMD), during swapout. This improves the swapping performance since all the bookking & I/O submission happens once per large folio. More details at [1].
PowerPC Book3S64 could not enable this before because PMD_ORDER is selected at runtime depending upon the chosen MMU. The earlier patches in this series turn SWAPFILE_CLUSTER into a runtime value and introduce an ARCH_MAX_PMD_ORDER upperbound override for SWAP_NR_ORDERS. With those changes, we can now enable THP SWAP for Book3S64.
This increases bandwidth throughput with zram backend for swapout by 40-50% with Radix and 100-130% with Hash (Tested by Sayali)
[1]: https://lore.kernel.org/all/[email protected]/
--- arch/powerpc/platforms/Kconfig.cputype | 1 + 1 file changed, 1 insertion(+)
arch/powerpc/platforms/Kconfig.cputype +1 -0
@@ -113,6 +113,7 @@ config PPC_THP select HAVE_ARCH_TRANSPARENT_HUGEPAGE select HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE + select ARCH_WANTS_THP_SWAP if TRANSPARENT_HUGEPAGE choice prompt "CPU selection"
--
2.39.5