Skip to content

Commit 7a2e7ae

Browse files
Kemeng Shiakpm00
authored andcommitted
mm: swap: use correct step in loop to wait all clusters in wait_for_allocation()
Use correct step in loop to wait all clusters in wait_for_allocation(). If we miss some cluster in wait_for_allocation(), use after free may occur as follows: shmem_writepage swapoff folio_alloc_swap get_swap_pages scan_swap_map_slots cluster_alloc_swap_entry alloc_swap_scan_cluster cluster_alloc_range /* SWP_WRITEOK is valid */ if (!(si->flags & SWP_WRITEOK)) ... del_from_avail_list(p, true); ... /* miss the cluster in shmem_writepage */ wait_for_allocation() ... try_to_unuse() memset(si->swap_map + start, usage, nr_pages); swap_range_alloc(si, nr_pages); ci->count += nr_pages; /* return a valid entry */ ... exit_swap_address_space(p->type); ... ... add_to_swap_cache /* dereference swap_address_space(entry) which is NULL */ xas_lock_irq(&xas); Link: https://lkml.kernel.org/r/20250222160850.505274-3-shikemeng@huaweicloud.com Fixes: 9a0ddeb ("mm, swap: hold a reference during scan and cleanup flag usage") Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com> Reviewed-by: Kairui Song <kasong@tencent.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 51f271c commit 7a2e7ae

File tree

1 file changed

+0
-1
lines changed

1 file changed

+0
-1
lines changed

mm/swapfile.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2645,7 +2645,6 @@ static void wait_for_allocation(struct swap_info_struct *si)
26452645
for (offset = 0; offset < end; offset += SWAPFILE_CLUSTER) {
26462646
ci = lock_cluster(si, offset);
26472647
unlock_cluster(ci);
2648-
offset += SWAPFILE_CLUSTER;
26492648
}
26502649
}
26512650

0 commit comments

Comments
 (0)