📋 Description
In the Linux kernel, the following vulnerability has been resolved:<br /> <br /> mm/swap: fix race when skipping swapcache<br /> <br /> When skipping swapcache for SWP_SYNCHRONOUS_IO, if two or more threads<br /> swapin the same entry at the same time, they get different pages (A, B). <br /> Before one thread (T0) finishes the swapin and installs page (A) to the<br /> PTE, another thread (T1) could finish swapin of page (B), swap_free the<br /> entry, then swap out the possibly modified page reusing the same entry. <br /> It breaks the pte_same check in (T0) because PTE value is unchanged,<br /> causing ABA problem. Thread (T0) will install a stalled page (A) into the<br /> PTE and cause data corruption.<br /> <br /> One possible callstack is like this:<br /> <br /> CPU0 CPU1<br /> ---- ----<br /> do_swap_page() do_swap_page() with same entry<br /> <direct swapin path> <direct swapin path><br /> <alloc page A> <alloc page B><br /> swap_read_folio() <- read to page A swap_read_folio() <- read to page B<br /> <slow on later locks or interrupt> <finished swapin first><br /> ... set_pte_at()<br /> swap_free() <- entry is free<br /> <write to page B, now page A stalled><br /> <swap out page B to same swap entry><br /> pte_same() <- Check pass, PTE seems<br /> unchanged, but page A<br /> is stalled!<br /> swap_free() <- page B content lost!<br /> set_pte_at() <- staled page A installed!<br /> <br /> And besides, for ZRAM, swap_free() allows the swap device to discard the<br /> entry content, so even if page (B) is not modified, if swap_read_folio()<br /> on CPU0 happens later than swap_free() on CPU1, it may also cause data<br /> loss.<br /> <br /> To fix this, reuse swapcache_prepare which will pin the swap entry using<br /> the cache flag, and allow only one thread to swap it in, also prevent any<br /> parallel code from putting the entry in the cache. Release the pin after<br /> PT unlocked.<br /> <br /> Racers just loop and wait since it's a rare and very short event. A<br /> schedule_timeout_uninterruptible(1) call is added to avoid repeated page<br /> faults wasting too much CPU, causing livelock or adding too much noise to<br /> perf statistics. A similar livelock issue was described in commit<br /> 029c4628b2eb ("mm: swap: get rid of livelock in swapin readahead")<br /> <br /> Reproducer:<br /> <br /> This race issue can be triggered easily using a well constructed<br /> reproducer and patched brd (with a delay in read path) [1]:<br /> <br /> With latest 6.8 mainline, race caused data loss can be observed easily:<br /> $ gcc -g -lpthread test-thread-swap-race.c && ./a.out<br /> Polulating 32MB of memory region...<br /> Keep swapping out...<br /> Starting round 0...<br /> Spawning 65536 workers...<br /> 32746 workers spawned, wait for done...<br /> Round 0: Error on 0x5aa00, expected 32746, got 32743, 3 data loss!<br /> Round 0: Error on 0x395200, expected 32746, got 32743, 3 data loss!<br /> Round 0: Error on 0x3fd000, expected 32746, got 32737, 9 data loss!<br /> Round 0 Failed, 15 data loss!<br /> <br /> This reproducer spawns multiple threads sharing the same memory region<br /> using a small swap device. Every two threads updates mapped pages one by<br /> one in opposite direction trying to create a race, with one dedicated<br /> thread keep swapping out the data out using madvise.<br /> <br /> The reproducer created a reproduce rate of about once every 5 minutes, so<br /> the race should be totally possible in production.<br /> <br /> After this patch, I ran the reproducer for over a few hundred rounds and<br /> no data loss observed.<br /> <br /> Performance overhead is minimal, microbenchmark swapin 10G from 32G<br /> zram:<br /> <br /> Before: 10934698 us<br /> After: 11157121 us<br /> Cached: 13155355 us (Dropping SWP_SYNCHRONOUS_IO flag)<br /> <br /> [kasong@tencent.com: v4]
