This makes SWAPFILE_CLUSTER as a runtime value. Architectures like powerpc book3s64 has HPAGE_PMD_NR, which is derived at runtime depending upon which chosen mmu. 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.
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(-) diff --git a/mm/swap.h b/mm/swap.h index 77d2d14eda42..956879a69ddd 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -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 diff --git a/mm/swap_table.h b/mm/swap_table.h index e6613e62f8d0..90e2a7852300 100644 --- a/mm/swap_table.h +++ b/mm/swap_table.h @@ -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 diff --git a/mm/swapfile.c b/mm/swapfile.c index 78b49b0658ad..016a5aa0cb93 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -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; +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
