mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-09-22 09:34:56 +02:00
mm: shmem: fix potential livelock issue for shmem direct swapin
When skipping swapcache for synchronous IO swap devices, swapcache_prepare() is used to prevent parallel swapin from proceeding with the swap cache flag. However, on PREEMPT kernels this can lead to a livelock, as reported by Chao[1]: Thread A starts direct swapin of a shmem folio and calls swapcache_prepare() to set SWAP_HAS_CACHE. It may then be preempted inside workingset_refault(). Meanwhile, a higher priority thread B also attempts direct swapin of the same shmem swap entry. Since swapcache_prepare() already marks the entry, thread B repeatedly gets -EEXIST and busy-loops waiting for thread A to finish. But as thread B runs at higher priority, thread A cannot preempt it, resulting in starvation and a livelock. Fix it by yielding the CPU with schedule_timeout_uninterruptible(1) when swapcache_prepare() fails, following the same approach used in commit029c4628b2("mm: swap: get rid of livelock in swapin readahead") and commit13ddaf26be("mm/swap: fix race when skipping swapcache"). However, commit01626a1823("mm: avoid unconditional one-tick sleep when swapcache_prepare fails") found that the unconditional one-tick sleep can cause UI stuttering on latency-sensitive Android devices. So we can follow the same approach by adding a waitqueue to wake up tasks when needed, instead of always sleeping for a full tick. Note that mainline does not have this potential issue, which has already been resolved by Kairui's swap refactoring work[2]. [1] https://lore.kernel.org/all/700a2cbf90a2484f979aac858f08f5d4@xiaomi.com/ [2] https://lore.kernel.org/all/20260517-swap-table-p4-v5-0-88ae43e064c7@tencent.com/ Fixes:1dd44c0af4("mm: shmem: skip swapcache for swapin of synchronous swap device") Reported-by: Ma Chao <machao26@xiaomi.com> Closes: https://lore.kernel.org/all/700a2cbf90a2484f979aac858f08f5d4@xiaomi.com/ Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
9818bcae3c
commit
b7b2d2ccdb
+13
-1
@@ -2005,11 +2005,14 @@ unlock:
|
||||
return ERR_PTR(error);
|
||||
}
|
||||
|
||||
static DECLARE_WAIT_QUEUE_HEAD(shmem_swapcache_wq);
|
||||
|
||||
static struct folio *shmem_swap_alloc_folio(struct inode *inode,
|
||||
struct vm_area_struct *vma, pgoff_t index,
|
||||
swp_entry_t entry, int order, gfp_t gfp)
|
||||
{
|
||||
struct shmem_inode_info *info = SHMEM_I(inode);
|
||||
DECLARE_WAITQUEUE(wait, current);
|
||||
int nr_pages = 1 << order;
|
||||
struct folio *new;
|
||||
gfp_t alloc_gfp;
|
||||
@@ -2066,6 +2069,10 @@ retry:
|
||||
if (swapcache_prepare(entry, nr_pages)) {
|
||||
folio_put(new);
|
||||
new = ERR_PTR(-EEXIST);
|
||||
/* Relax a bit to prevent rapid repeated page faults */
|
||||
add_wait_queue(&shmem_swapcache_wq, &wait);
|
||||
schedule_timeout_uninterruptible(1);
|
||||
remove_wait_queue(&shmem_swapcache_wq, &wait);
|
||||
/* Try smaller folio to avoid cache conflict */
|
||||
goto fallback;
|
||||
}
|
||||
@@ -2423,6 +2430,8 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
|
||||
if (skip_swapcache) {
|
||||
folio->swap.val = 0;
|
||||
swapcache_clear(si, swap, nr_pages);
|
||||
if (waitqueue_active(&shmem_swapcache_wq))
|
||||
wake_up(&shmem_swapcache_wq);
|
||||
} else {
|
||||
swap_cache_del_folio(folio);
|
||||
}
|
||||
@@ -2442,8 +2451,11 @@ unlock:
|
||||
if (folio)
|
||||
folio_unlock(folio);
|
||||
failed_nolock:
|
||||
if (skip_swapcache)
|
||||
if (skip_swapcache) {
|
||||
swapcache_clear(si, folio->swap, folio_nr_pages(folio));
|
||||
if (waitqueue_active(&shmem_swapcache_wq))
|
||||
wake_up(&shmem_swapcache_wq);
|
||||
}
|
||||
if (folio)
|
||||
folio_put(folio);
|
||||
put_swap_device(si);
|
||||
|
||||
Reference in New Issue
Block a user