Whether is the race for SWP_SYNCHRONOUS_IO possible? (was Re: [PATCH v3 6/7] mm/swap, shmem: use unified swapin helper for shmem)

From: Huang, Ying
Date: Tue Jan 30 2024 - 21:53:47 EST


Hi, Minchan,

When I review the patchset from Kairui, I checked the code to skip swap
cache in do_swap_page() for swap device with SWP_SYNCHRONOUS_IO. Is the
following race possible? Where a page is swapped out to a swap device
with SWP_SYNCHRONOUS_IO and the swap count is 1. Then 2 threads of the
process runs on CPU0 and CPU1 as below. CPU0 is running do_swap_page().

CPU0 CPU1
---- ----
swap_cache_get_folio()
check sync io and swap count
alloc folio
swap_readpage()
folio_lock_or_retry()
swap in the swap entry
write page
swap out to same swap entry
pte_offset_map_lock()
check pte_same()
swap_free() <-- new content lost!
set_pte_at() <-- stale page!
folio_unlock()
pte_unmap_unlock()


Do I miss anything?

--
Best Regards,
Huang, Ying