Skip to content

Commit bbcef20

Browse files
ryncsngregkh
authored andcommitted
mm, swap: speed up hibernation allocation and writeout
[ Upstream commit 396f57b ] Since commit 0ff67f9 ("mm, swap: remove swap slot cache"), hibernation has been using the swap slot slow allocation path for simplification, which turns out might cause regression for some devices because the allocator now rotates clusters too often, leading to slower allocation and more random distribution of data. Fast allocation is not complex, so implement hibernation support as well. Test result with Samsung SSD 830 Series (SATA II, 3.0 Gbps) shows the performance is several times better [1]: 6.19: 324 seconds After this series: 35 seconds Link: https://lkml.kernel.org/r/20260216-hibernate-perf-v4-1-1ba9f0bf1ec9@tencent.com Link: https://lore.kernel.org/linux-mm/8b4bdcfa-ce3f-4e23-839f-31367df7c18f@gmx.de/ [1] Signed-off-by: Kairui Song <kasong@tencent.com> Fixes: 0ff67f9 ("mm, swap: remove swap slot cache") Reported-by: Carsten Grohmann <mail@carstengrohmann.de> Closes: https://lore.kernel.org/linux-mm/20260206121151.dea3633d1f0ded7bbf49c22e@linux-foundation.org/ Cc: Baoquan He <bhe@redhat.com> Cc: Barry Song <baohua@kernel.org> Cc: Chris Li <chrisl@kernel.org> Cc: Kemeng Shi <shikemeng@huaweicloud.com> Cc: Nhat Pham <nphamcs@gmail.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> [ adjusted helper signatures ] Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4d7b429 commit bbcef20

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

mm/swapfile.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,8 +2014,9 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr)
20142014

20152015
swp_entry_t get_swap_page_of_type(int type)
20162016
{
2017-
struct swap_info_struct *si = swap_type_to_info(type);
2018-
unsigned long offset;
2017+
struct swap_info_struct *pcp_si, *si = swap_type_to_info(type);
2018+
unsigned long pcp_offset, offset = SWAP_ENTRY_INVALID;
2019+
struct swap_cluster_info *ci;
20192020
swp_entry_t entry = {0};
20202021

20212022
if (!si)
@@ -2025,11 +2026,21 @@ swp_entry_t get_swap_page_of_type(int type)
20252026
if (get_swap_device_info(si)) {
20262027
if (si->flags & SWP_WRITEOK) {
20272028
/*
2028-
* Grab the local lock to be complaint
2029-
* with swap table allocation.
2029+
* Try the local cluster first if it matches the device. If
2030+
* not, try grab a new cluster and override local cluster.
20302031
*/
20312032
local_lock(&percpu_swap_cluster.lock);
2032-
offset = cluster_alloc_swap_entry(si, 0, 1);
2033+
pcp_si = this_cpu_read(percpu_swap_cluster.si[0]);
2034+
pcp_offset = this_cpu_read(percpu_swap_cluster.offset[0]);
2035+
if (pcp_si == si && pcp_offset) {
2036+
ci = swap_cluster_lock(si, pcp_offset);
2037+
if (cluster_is_usable(ci, 0))
2038+
offset = alloc_swap_scan_cluster(si, ci, pcp_offset, 0, 1);
2039+
else
2040+
swap_cluster_unlock(ci);
2041+
}
2042+
if (!offset)
2043+
offset = cluster_alloc_swap_entry(si, 0, 1);
20332044
local_unlock(&percpu_swap_cluster.lock);
20342045
if (offset)
20352046
entry = swp_entry(si->type, offset);

0 commit comments

Comments
 (0)