Hibernation takes whatever clusters are free when it runs, and an aged device may have no long run of them. At swapon, ask hibernation_reserve_pages() how much to hold back and set aside that many empty clusters in a row. Nothing uses the device yet, so this needs no locking. The run leaves si->pages and goes on no list, so the allocator never sees it. Hibernation hands it out whole, before any other slot. More than half of a device is refused. Assisted-by: Claude:claude-opus-5 Signed-off-by: Youngjun Park --- include/linux/swap.h | 2 ++ mm/swap_hibernate.c | 78 ++++++++++++++++++++++++++++++++++++++++---- mm/swapfile.c | 23 ++++++++++++- 3 files changed, 95 insertions(+), 8 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 94c894c7ad9d..66d59828b327 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -249,6 +249,8 @@ struct swap_info_struct { /* list of cluster that are fragmented or contented */ unsigned int pages; /* total of usable pages of swap */ atomic_long_t inuse_pages; /* number of those currently in use */ + unsigned int hib_reserve_start; /* clusters held back for hibernation */ + unsigned int hib_reserve_nr; /* how many, 0 for none */ struct swap_sequential_cluster *global_cluster; /* Use one global cluster for rotating device */ spinlock_t global_cluster_lock; /* Serialize usage of global cluster */ struct rb_root swap_extent_root;/* root of the swap extent rbtree */ diff --git a/mm/swap_hibernate.c b/mm/swap_hibernate.c index 23dc766f04cd..7475ae0966f3 100644 --- a/mm/swap_hibernate.c +++ b/mm/swap_hibernate.c @@ -5,10 +5,12 @@ */ /* - * The image gets whole free clusters first, then single slots from the - * allocator. Clusters it does not use go back when the session ends. + * The image gets the run held at swapon first, then whole free clusters, + * then single slots from the allocator. Free clusters it does not use go + * back when the session ends. */ static struct swap_info_struct *hib_si; +static unsigned int hib_reserve; /* clusters held back, still to hand out */ /* Free clusters set aside for the image, out of the allocator's reach. */ static LIST_HEAD(hib_free_clusters); @@ -38,6 +40,56 @@ static void hib_return_free_clusters(struct swap_info_struct *si) spin_unlock(&si->lock); } +static bool hib_cluster_reserved(struct swap_info_struct *si, unsigned int idx) +{ + return idx >= si->hib_reserve_start && + idx < si->hib_reserve_start + si->hib_reserve_nr; +} + +/* + * Hold a run of empty clusters back for the image. This runs at swapon + * before the device is in use, so it needs no lock. + * + * Return: the number of pages held, to be taken out of si->pages. + */ +static unsigned long hib_reserve_at_swapon(struct swap_info_struct *si, + struct swap_cluster_info *cluster_info, + unsigned long nr_clusters) +{ + unsigned long want, run = 0, i; + + si->hib_reserve_start = 0; + si->hib_reserve_nr = 0; + + if (!si->bdev) + return 0; + /* Swap files sharing a block device differ by their first block. */ + want = DIV_ROUND_UP(hibernation_reserve_pages(si->bdev->bd_dev, + first_se(si)->start_block), + SWAPFILE_CLUSTER); + if (!want) + return 0; + if (want > nr_clusters / 2) { + pr_warn("swapon: %lu clusters for the hibernation image is more than half of the device\n", + want); + return 0; + } + + for (i = 0; i < nr_clusters && run < want; i++) + run = cluster_info[i].count ? 0 : run + 1; + if (run < want) { + pr_warn("swapon: no run of %lu clusters for the hibernation image\n", + want); + return 0; + } + + si->hib_reserve_start = i - want; + si->hib_reserve_nr = want; + pr_info("swapon: holding %lu clusters at %u back for the hibernation image\n", + want, si->hib_reserve_start); + return want * SWAPFILE_CLUSTER; +} + /* * Take a whole free cluster. It gets no swap table, so cluster_is_usable() * keeps the allocator away from it. @@ -130,6 +182,7 @@ static void hib_session_begin(struct swap_info_struct *si) if (hib_si) hib_return_free_clusters(hib_si); hib_si = si; + hib_reserve = si->hib_reserve_nr; } static void hib_session_end(void) @@ -189,6 +242,13 @@ swp_entry_t swap_alloc_hibernation_slot(unsigned int *nr) if (WARN_ON_ONCE(!si)) return entry; + if (hib_reserve) { + *nr = hib_reserve * SWAPFILE_CLUSTER; + hib_reserve = 0; + return swp_entry(si->type, + si->hib_reserve_start * SWAPFILE_CLUSTER); + } + len = hib_take_free(si, &start); if (!len) { start = hib_alloc_slot(si); @@ -218,10 +278,13 @@ void swap_free_hibernation_slot(swp_entry_t entry, unsigned int nr) ci_off = offset % SWAPFILE_CLUSTER; count = min(nr, SWAPFILE_CLUSTER - ci_off); ci = swap_cluster_lock(si, offset); - if (cluster_table_is_alloced(ci)) - __swap_cluster_free_entries(si, ci, ci_off, count); - else - hib_put_cluster_slots(si, ci, ci_off, count); + /* A cluster held for the image is left as it is. */ + if (!hib_cluster_reserved(si, cluster_index(si, ci))) { + if (cluster_table_is_alloced(ci)) + __swap_cluster_free_entries(si, ci, ci_off, count); + else + hib_put_cluster_slots(si, ci, ci_off, count); + } swap_cluster_unlock(ci); offset += count; nr -= count; @@ -472,7 +535,8 @@ unsigned int count_swap_pages(int type, int free) spin_lock(&sis->lock); if (sis->flags & SWP_WRITEOK) { - n = sis->pages; + /* the run held back is there for the image */ + n = sis->pages + sis->hib_reserve_nr * SWAPFILE_CLUSTER; if (free) n -= swap_usage_in_pages(sis); } diff --git a/mm/swapfile.c b/mm/swapfile.c index 98ed75708661..3b79cec6fd27 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2182,6 +2182,18 @@ void swap_put_entries_direct(swp_entry_t entry, int nr) #ifdef CONFIG_HIBERNATION #include "swap_hibernate.c" +#else +static unsigned long hib_reserve_at_swapon(struct swap_info_struct *si, + struct swap_cluster_info *cluster_info, + unsigned long nr_clusters) +{ + return 0; +} + +static bool hib_cluster_reserved(struct swap_info_struct *si, unsigned int idx) +{ + return false; +} #endif /* CONFIG_HIBERNATION */ static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte) @@ -3379,6 +3391,13 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, goto err; } + /* + * Whatever is held back for a hibernation image leaves si->pages, so + * that it is never advertised as swap, and it goes on no list below, + * so that the allocator cannot find it. + */ + si->pages -= hib_reserve_at_swapon(si, cluster_info, nr_clusters); + INIT_LIST_HEAD(&si->free_clusters); INIT_LIST_HEAD(&si->full_clusters); INIT_LIST_HEAD(&si->discard_clusters); @@ -3391,7 +3410,9 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, for (i = 0; i < nr_clusters; i++) { struct swap_cluster_info *ci = &cluster_info[i]; - if (ci->count) { + if (hib_cluster_reserved(si, i)) { + ci->flags = CLUSTER_FLAG_NONE; + } else if (ci->count) { ci->flags = CLUSTER_FLAG_NONFULL; list_add_tail(&ci->list, &si->nonfull_clusters[0]); } else { -- 2.48.1