]> www.infradead.org Git - users/jedix/linux-maple.git/commit
mm: swap: use correct step in loop to wait all clusters in wait_for_allocation()
authorKemeng Shi <shikemeng@huaweicloud.com>
Sat, 22 Feb 2025 16:08:46 +0000 (00:08 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 4 Mar 2025 05:48:28 +0000 (21:48 -0800)
commit931de5a9d22f914ff0f21af52a631438d1cda529
tree4576e8838fbb88ebae5e727b514b71cdd3fd979f
parentd8fee650bd5b388715bf5f18a7cb3bdabf3bb48d
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: 9a0ddeb79880 ("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>
mm/swapfile.c