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

Configure Feed

Select the types of activity you want to include in your feed.

mm: swap: rename __swap_[entry/entries]_free[_locked] to swap_[entry/entries]_put[_locked]

Patch series "Minor cleanups and improvements to swap freeing code", v4.

This series contains some cleanups and improvements which are made
during learning swapfile. Here is a summary of the changes:

1. Function naming improvments.

- Use "put" instead of "free" to name functions which only do actual
free when count drops to zero.

- Use "entry" to name function only frees one swap slot. Use
"entries" to name function could may free multi swap slots within one
cluster. Use "_nr" suffix to name function which could free multi
swap slots spanning cross multi clusters.

2. Eliminate the need to set swap slot to intermediate SWAP_HAS_CACHE
value before do actual free by using swap_entry_range_free()

3. Add helpers swap_entries_put_map() and swap_entries_put_cache() as
a general-purpose routine to free swap entries within a single cluster
which will try batch-remove first and fallback to put eatch entry
indvidually with cluster lock acquired/released only once. By using
these helpers, we could remove repeated code, levarage batch-remove in
more cases and aoivd to acquire/release cluster lock for each single
swap entry.


This patch (of 8):

In __swap_entry_free[_locked] and __swap_entries_free, we decrease count
first and only free swap entry if count drops to zero. This behavior is
more akin to a put() operation rather than a free() operation. Therefore,
rename these functions with "put" instead of "free". Additionally, add
"_nr" suffix to swap_entries_put to indicate the input range may span swap
clusters.

Link: https://lkml.kernel.org/r/20250325162528.68385-1-shikemeng@huaweicloud.com
Link: https://lkml.kernel.org/r/20250325162528.68385-2-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
9c1c38bc ac26920d

+14 -14
+14 -14
mm/swapfile.c
··· 1355 1355 return NULL; 1356 1356 } 1357 1357 1358 - static unsigned char __swap_entry_free_locked(struct swap_info_struct *si, 1359 - unsigned long offset, 1360 - unsigned char usage) 1358 + static unsigned char swap_entry_put_locked(struct swap_info_struct *si, 1359 + unsigned long offset, 1360 + unsigned char usage) 1361 1361 { 1362 1362 unsigned char count; 1363 1363 unsigned char has_cache; ··· 1461 1461 return NULL; 1462 1462 } 1463 1463 1464 - static unsigned char __swap_entry_free(struct swap_info_struct *si, 1465 - swp_entry_t entry) 1464 + static unsigned char swap_entry_put(struct swap_info_struct *si, 1465 + swp_entry_t entry) 1466 1466 { 1467 1467 struct swap_cluster_info *ci; 1468 1468 unsigned long offset = swp_offset(entry); 1469 1469 unsigned char usage; 1470 1470 1471 1471 ci = lock_cluster(si, offset); 1472 - usage = __swap_entry_free_locked(si, offset, 1); 1472 + usage = swap_entry_put_locked(si, offset, 1); 1473 1473 if (!usage) 1474 1474 swap_entry_range_free(si, ci, swp_entry(si->type, offset), 1); 1475 1475 unlock_cluster(ci); ··· 1477 1477 return usage; 1478 1478 } 1479 1479 1480 - static bool __swap_entries_free(struct swap_info_struct *si, 1481 - swp_entry_t entry, int nr) 1480 + static bool swap_entries_put_nr(struct swap_info_struct *si, 1481 + swp_entry_t entry, int nr) 1482 1482 { 1483 1483 unsigned long offset = swp_offset(entry); 1484 1484 unsigned int type = swp_type(entry); ··· 1509 1509 fallback: 1510 1510 for (i = 0; i < nr; i++) { 1511 1511 if (data_race(si->swap_map[offset + i])) { 1512 - count = __swap_entry_free(si, swp_entry(type, offset + i)); 1512 + count = swap_entry_put(si, swp_entry(type, offset + i)); 1513 1513 if (count == SWAP_HAS_CACHE) 1514 1514 has_cache = true; 1515 1515 } else { ··· 1560 1560 1561 1561 ci = lock_cluster(si, offset); 1562 1562 do { 1563 - if (!__swap_entry_free_locked(si, offset, usage)) 1563 + if (!swap_entry_put_locked(si, offset, usage)) 1564 1564 swap_entry_range_free(si, ci, swp_entry(si->type, offset), 1); 1565 1565 } while (++offset < end); 1566 1566 unlock_cluster(ci); ··· 1607 1607 swap_entry_range_free(si, ci, entry, size); 1608 1608 else { 1609 1609 for (int i = 0; i < size; i++, entry.val++) { 1610 - if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) 1610 + if (!swap_entry_put_locked(si, offset + i, SWAP_HAS_CACHE)) 1611 1611 swap_entry_range_free(si, ci, entry, 1); 1612 1612 } 1613 1613 } ··· 1806 1806 /* 1807 1807 * First free all entries in the range. 1808 1808 */ 1809 - any_only_cache = __swap_entries_free(si, entry, nr); 1809 + any_only_cache = swap_entries_put_nr(si, entry, nr); 1810 1810 1811 1811 /* 1812 1812 * Short-circuit the below loop if none of the entries had their ··· 1819 1819 * Now go back over the range trying to reclaim the swap cache. This is 1820 1820 * more efficient for large folios because we will only try to reclaim 1821 1821 * the swap once per folio in the common case. If we do 1822 - * __swap_entry_free() and __try_to_reclaim_swap() in the same loop, the 1822 + * swap_entry_put() and __try_to_reclaim_swap() in the same loop, the 1823 1823 * latter will get a reference and lock the folio for every individual 1824 1824 * page but will only succeed once the swap slot for every subpage is 1825 1825 * zero. ··· 3789 3789 * into, carry if so, or else fail until a new continuation page is allocated; 3790 3790 * when the original swap_map count is decremented from 0 with continuation, 3791 3791 * borrow from the continuation and report whether it still holds more. 3792 - * Called while __swap_duplicate() or caller of __swap_entry_free_locked() 3792 + * Called while __swap_duplicate() or caller of swap_entry_put_locked() 3793 3793 * holds cluster lock. 3794 3794 */ 3795 3795 static bool swap_count_continued(struct swap_info_struct *si,