Skip to content

Commit 01626a1

Browse files
Barry Songakpm00
authored andcommitted
mm: avoid unconditional one-tick sleep when swapcache_prepare fails
Commit 13ddaf2 ("mm/swap: fix race when skipping swapcache") introduced an unconditional one-tick sleep when `swapcache_prepare()` fails, which has led to reports of UI stuttering on latency-sensitive Android devices. To address this, we can use a waitqueue to wake up tasks that fail `swapcache_prepare()` sooner, instead of always sleeping for a full tick. While tasks may occasionally be woken by an unrelated `do_swap_page()`, this method is preferable to two scenarios: rapid re-entry into page faults, which can cause livelocks, and multiple millisecond sleeps, which visibly degrade user experience. Oven's testing shows that a single waitqueue resolves the UI stuttering issue. If a 'thundering herd' problem becomes apparent later, a waitqueue hash similar to `folio_wait_table[PAGE_WAIT_TABLE_SIZE]` for page bit locks can be introduced. [[email protected]: wake_up only when swapcache_wq waitqueue is active] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: 13ddaf2 ("mm/swap: fix race when skipping swapcache") Signed-off-by: Barry Song <[email protected]> Reported-by: Oven Liyang <[email protected]> Tested-by: Oven Liyang <[email protected]> Cc: Kairui Song <[email protected]> Cc: "Huang, Ying" <[email protected]> Cc: Yu Zhao <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Chris Li <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Yosry Ahmed <[email protected]> Cc: SeongJae Park <[email protected]> Cc: Kalesh Singh <[email protected]> Cc: Suren Baghdasaryan <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 1834300 commit 01626a1

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

mm/memory.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4187,6 +4187,8 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
41874187
}
41884188
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
41894189

4190+
static DECLARE_WAIT_QUEUE_HEAD(swapcache_wq);
4191+
41904192
/*
41914193
* We enter with non-exclusive mmap_lock (to exclude vma changes,
41924194
* but allow concurrent faults), and pte mapped but not yet locked.
@@ -4199,6 +4201,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
41994201
{
42004202
struct vm_area_struct *vma = vmf->vma;
42014203
struct folio *swapcache, *folio = NULL;
4204+
DECLARE_WAITQUEUE(wait, current);
42024205
struct page *page;
42034206
struct swap_info_struct *si = NULL;
42044207
rmap_t rmap_flags = RMAP_NONE;
@@ -4297,7 +4300,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
42974300
* Relax a bit to prevent rapid
42984301
* repeated page faults.
42994302
*/
4303+
add_wait_queue(&swapcache_wq, &wait);
43004304
schedule_timeout_uninterruptible(1);
4305+
remove_wait_queue(&swapcache_wq, &wait);
43014306
goto out_page;
43024307
}
43034308
need_clear_cache = true;
@@ -4604,8 +4609,11 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
46044609
pte_unmap_unlock(vmf->pte, vmf->ptl);
46054610
out:
46064611
/* Clear the swap cache pin for direct swapin after PTL unlock */
4607-
if (need_clear_cache)
4612+
if (need_clear_cache) {
46084613
swapcache_clear(si, entry, nr_pages);
4614+
if (waitqueue_active(&swapcache_wq))
4615+
wake_up(&swapcache_wq);
4616+
}
46094617
if (si)
46104618
put_swap_device(si);
46114619
return ret;
@@ -4620,8 +4628,11 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
46204628
folio_unlock(swapcache);
46214629
folio_put(swapcache);
46224630
}
4623-
if (need_clear_cache)
4631+
if (need_clear_cache) {
46244632
swapcache_clear(si, entry, nr_pages);
4633+
if (waitqueue_active(&swapcache_wq))
4634+
wake_up(&swapcache_wq);
4635+
}
46254636
if (si)
46264637
put_swap_device(si);
46274638
return ret;

0 commit comments

Comments
 (0)