From: Baoquan He swapoff of a physical device drops the xswap entries that were written to it. The charge taken when the backend was allocated is released by xswap_release_slot_backend() when the xswap slot itself is freed, or by xswap_backend_free() when an allocation is rolled back. The record can be cleared before either runs: - the swapoff path clears it under the cluster lock, but uncharged outside it: a concurrent slot free can uncharge twice, or clear the id before the swapoff path reads it; - xswap_backend_free() clears it through __xswap_backend_unlink(), which does nothing when the record no longer matches, but it then uncharged unconditionally. Capture the owner id, clear the record, and uncharge only when the record still points at this entry, under the same cluster lock. Signed-off-by: Baoquan He --- mm/swapfile.c | 56 ++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index baebd30cf889..5de905970668 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -105,7 +105,6 @@ static int xswap_reclaim_backing(struct swap_info_struct *si, unsigned long offset, swp_entry_t xentry); static unsigned short xswap_entry_id(struct swap_cluster_info *ci, unsigned long off); -static void xswap_backend_uncharge(swp_entry_t entry, unsigned int nr); static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys, unsigned int order); @@ -3446,11 +3445,7 @@ static void xswap_release_slot_backend(struct swap_info_struct *si, if (psi) xswap_free_phys_slot(psi, entry, phys); - /* - * ci->lock is held here, so read the owner out of the swap table - * directly. xswap_backend_uncharge() would take the same lock - * again, and a spinlock is not recursive. - */ + /* ci->lock is held here, so read the owner out of the swap table directly. */ if (id) mem_cgroup_swap_uncharge(id, 1); } @@ -3683,17 +3678,23 @@ static int xswap_unuse_rmap(struct swap_info_struct *si, unsigned long offset) folio_put(folio); /* - * The slot is going away, so drop the record that names it. The - * mapped range cannot shrink while the device is pinned, so @xoff - * still has a cluster here whenever @drop is set. + * Clear the record and uncharge under one lock. A concurrent slot + * free then cannot uncharge twice or clear the id before we read it. + * The mapped range cannot shrink while the device is pinned, so + * @xoff still has a cluster here whenever @drop is set. */ if (drop) { xci = swap_cluster_lock(xsi, xoff); if (xci) { - if (xswap_slot_backend(xci, xoff % SWAPFILE_CLUSTER).val == - phys.val) - WRITE_ONCE(xci->xs_table[xoff % SWAPFILE_CLUSTER], - 0); + unsigned int slot = xoff % SWAPFILE_CLUSTER; + unsigned short id; + + if (xswap_slot_backend(xci, slot).val == phys.val) { + id = xswap_entry_id(xci, slot); + WRITE_ONCE(xci->xs_table[slot], 0); + if (id) + mem_cgroup_swap_uncharge(id, 1); + } swap_cluster_unlock(xci); } } @@ -4993,23 +4994,10 @@ static unsigned short xswap_entry_id(struct swap_cluster_info *ci, return __swap_cgroup_get(ci, off % SWAPFILE_CLUSTER); } -static void xswap_backend_uncharge(swp_entry_t entry, unsigned int nr) -{ - struct swap_info_struct *si = __swap_entry_to_info(entry); - struct swap_cluster_info *ci; - unsigned long off = swp_offset(entry); - unsigned short id = 0; - - ci = swap_cluster_lock(si, off); - if (ci) { - id = xswap_entry_id(ci, off); - swap_cluster_unlock(ci); - } - if (id) - mem_cgroup_swap_uncharge(id, nr); -} - -/* Drop the physical slot range and the reverse mapping, without any charging. */ +/* + * Drop the physical slot range and the reverse mapping, and uncharge what + * we clear. + */ static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys, unsigned int order) { @@ -5028,8 +5016,13 @@ static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys, unsigned int slot = (off + i) % SWAPFILE_CLUSTER; unsigned long *xs = ci->xs_table; - if (xs && xs[slot] == phys.val + i) + if (xs && xs[slot] == phys.val + i) { + unsigned short id = xswap_entry_id(ci, slot); + WRITE_ONCE(xs[slot], 0); + if (id) + mem_cgroup_swap_uncharge(id, 1); + } swap_cluster_unlock(ci); } @@ -5044,7 +5037,6 @@ static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys, void xswap_backend_free(swp_entry_t entry, swp_entry_t phys, unsigned int order) { __xswap_backend_unlink(entry, phys, order); - xswap_backend_uncharge(entry, 1UL << order); } static int xswap_map_clusters(struct swap_info_struct *si, -- 2.54.0