Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: swap: replace cluster_swap_free_nr() with swap_entries_put_[map/cache]()

Replace cluster_swap_free_nr() with swap_entries_put_[map/cache]() to
remove repeat code and leverage batch-remove for entries with last flag.
After removing cluster_swap_free_nr, only functions with "_nr" suffix
could free entries spanning cross clusters. Add corresponding description
in comment of swap_entries_put_map_nr() as is first function with "_nr"
suffix and have a non-suffix variant function swap_entries_put_map().

Link: https://lkml.kernel.org/r/20250325162528.68385-9-shikemeng@huaweicloud.com
Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
Reviewed-by: Tim Chen <tim.c.chen@linux.intel.com>
Reviewed-by: Baoquan He <bhe@redhat.com>
Cc: Kairui Song <kasong@tencent.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Kemeng Shi and committed by
Andrew Morton
ec9827cd d4f8000b

+11 -19
+11 -19
mm/swapfile.c
··· 1520 1520 1521 1521 } 1522 1522 1523 + /* 1524 + * Only functions with "_nr" suffix are able to free entries spanning 1525 + * cross multi clusters, so ensure the range is within a single cluster 1526 + * when freeing entries with functions without "_nr" suffix. 1527 + */ 1523 1528 static bool swap_entries_put_map_nr(struct swap_info_struct *si, 1524 1529 swp_entry_t entry, int nr) 1525 1530 { ··· 1586 1581 partial_free_cluster(si, ci); 1587 1582 } 1588 1583 1589 - static void cluster_swap_free_nr(struct swap_info_struct *si, 1590 - unsigned long offset, int nr_pages, 1591 - unsigned char usage) 1592 - { 1593 - struct swap_cluster_info *ci; 1594 - unsigned long end = offset + nr_pages; 1595 - 1596 - ci = lock_cluster(si, offset); 1597 - do { 1598 - swap_entry_put_locked(si, ci, swp_entry(si->type, offset), 1599 - usage); 1600 - } while (++offset < end); 1601 - unlock_cluster(ci); 1602 - } 1603 - 1604 1584 /* 1605 1585 * Caller has made sure that the swap device corresponding to entry 1606 1586 * is still around or has not been recycled. ··· 1602 1612 1603 1613 while (nr_pages) { 1604 1614 nr = min_t(int, nr_pages, SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); 1605 - cluster_swap_free_nr(sis, offset, nr, 1); 1615 + swap_entries_put_map(sis, swp_entry(sis->type, offset), nr); 1606 1616 offset += nr; 1607 1617 nr_pages -= nr; 1608 1618 } ··· 3648 3658 return __swap_duplicate(entry, SWAP_HAS_CACHE, nr); 3649 3659 } 3650 3660 3661 + /* 3662 + * Caller should ensure entries belong to the same folio so 3663 + * the entries won't span cross cluster boundary. 3664 + */ 3651 3665 void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr) 3652 3666 { 3653 - unsigned long offset = swp_offset(entry); 3654 - 3655 - cluster_swap_free_nr(si, offset, nr, SWAP_HAS_CACHE); 3667 + swap_entries_put_cache(si, entry, nr); 3656 3668 } 3657 3669 3658 3670 struct swap_info_struct *swp_swap_info(swp_entry_t entry)