mirror of
https://github.com/BobTheBlinker/android_kernel_motorola_sm6375.git
synced 2026-10-28 18:51:56 -04:00
BACKPORT: mm: don't be stuck to rmap lock on reclaim path
The rmap locks(i_mmap_rwsem and anon_vma->root->rwsem) could be contended
under memory pressure if processes keep working on their vmas(e.g., fork,
mmap, munmap). It makes reclaim path stuck. In our real workload traces,
we see kswapd is waiting the lock for 300ms+(worst case, a sec) and it
makes other processes entering direct reclaim, which were also stuck on
the lock.
This patch makes lru aging path try_lock mode like shink_page_list so the
reclaim context will keep working with next lru pages without being stuck.
if it found the rmap lock contended, it rotates the page back to head of
lru in both active/inactive lrus to make them consistent behavior, which
is basic starting point rather than adding more heristic.
Since this patch introduces a new "contended" field as out-param along
with try_lock in-param in rmap_walk_control, it's not immutable any longer
if the try_lock is set so remove const keywords on rmap related functions.
Since rmap walking is already expensive operation, I doubt the const
would help sizable benefit( And we didn't have it until 5.17).
In a heavy app workload in Android, trace shows following statistics. It
almost removes rmap lock contention from reclaim path.
Martin Liu reported:
Before:
max_dur(ms) min_dur(ms) max-min(dur)ms avg_dur(ms) sum_dur(ms) count blocked_function
1632 0 1631 151.542173 31672 209 page_lock_anon_vma_read
601 0 601 145.544681 28817 198 rmap_walk_file
After:
max_dur(ms) min_dur(ms) max-min(dur)ms avg_dur(ms) sum_dur(ms) count blocked_function
NaN NaN NaN NaN NaN 0.0 NaN
0 0 0 0.127645 1 12 rmap_walk_file
[[email protected]: add comment, per Matthew]
Link: https://lkml.kernel.org/r/[email protected]
Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Minchan Kim <[email protected]>
Acked-by: Johannes Weiner <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: John Dias <[email protected]>
Cc: Tim Murray <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Vladimir Davydov <[email protected]>
Cc: Martin Liu <[email protected]>
Cc: Minchan Kim <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Conflicts:
folio->page
(cherry picked from commit 6d4675e601357834dadd2ba1d803f6484596015c)
Bug: 239681156
Bug: 252333201
Bug: 265247508
Signed-off-by: Minchan Kim <[email protected]>
Change-Id: I0c63e0291120c8a1b5f2d83b8a7b210cb56c27a2
Signed-off-by: chenxin <[email protected]>
Signed-off-by: guchongchong <[email protected]>
(cherry picked from commit a0eae55f26a0cbdd828db226592093ad2142889c)
This commit is contained in:
parent
a69a8cd3c5
commit
8912db2538
8 changed files with 80 additions and 21 deletions
45
mm/rmap.c
45
mm/rmap.c
|
|
@ -503,9 +503,11 @@ out:
|
|||
*
|
||||
* Its a little more complex as it tries to keep the fast path to a single
|
||||
* atomic op -- the trylock. If we fail the trylock, we fall back to getting a
|
||||
* reference like with page_get_anon_vma() and then block on the mutex.
|
||||
* reference like with page_get_anon_vma() and then block on the mutex
|
||||
* on !rwc->try_lock case.
|
||||
*/
|
||||
struct anon_vma *page_lock_anon_vma_read(struct page *page)
|
||||
struct anon_vma *page_lock_anon_vma_read(struct page *page,
|
||||
struct rmap_walk_control *rwc)
|
||||
{
|
||||
struct anon_vma *anon_vma = NULL;
|
||||
struct anon_vma *root_anon_vma;
|
||||
|
|
@ -533,6 +535,12 @@ struct anon_vma *page_lock_anon_vma_read(struct page *page)
|
|||
goto out;
|
||||
}
|
||||
|
||||
if (rwc && rwc->try_lock) {
|
||||
anon_vma = NULL;
|
||||
rwc->contended = true;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* trylock failed, we got to sleep */
|
||||
if (!atomic_inc_not_zero(&anon_vma->refcount)) {
|
||||
anon_vma = NULL;
|
||||
|
|
@ -828,8 +836,10 @@ static bool invalid_page_referenced_vma(struct vm_area_struct *vma, void *arg)
|
|||
* @memcg: target memory cgroup
|
||||
* @vm_flags: collect encountered vma->vm_flags who actually referenced the page
|
||||
*
|
||||
* Quick test_and_clear_referenced for all mappings to a page,
|
||||
* returns the number of ptes which referenced the page.
|
||||
* Quick test_and_clear_referenced for all mappings of a page,
|
||||
*
|
||||
* Return: The number of mappings which referenced the page. Return -1 if
|
||||
* the function bailed out due to rmap lock contention.
|
||||
*/
|
||||
int page_referenced(struct page *page,
|
||||
int is_locked,
|
||||
|
|
@ -845,6 +855,7 @@ int page_referenced(struct page *page,
|
|||
.rmap_one = page_referenced_one,
|
||||
.arg = (void *)&pra,
|
||||
.anon_lock = page_lock_anon_vma_read,
|
||||
.try_lock = true,
|
||||
};
|
||||
|
||||
*vm_flags = 0;
|
||||
|
|
@ -875,7 +886,7 @@ int page_referenced(struct page *page,
|
|||
if (we_locked)
|
||||
unlock_page(page);
|
||||
|
||||
return pra.referenced;
|
||||
return rwc.contended ? -1 : pra.referenced;
|
||||
}
|
||||
|
||||
static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma,
|
||||
|
|
@ -1810,7 +1821,7 @@ static struct anon_vma *rmap_walk_anon_lock(struct page *page,
|
|||
struct anon_vma *anon_vma;
|
||||
|
||||
if (rwc->anon_lock)
|
||||
return rwc->anon_lock(page);
|
||||
return rwc->anon_lock(page, rwc);
|
||||
|
||||
/*
|
||||
* Note: remove_migration_ptes() cannot use page_lock_anon_vma_read()
|
||||
|
|
@ -1822,7 +1833,17 @@ static struct anon_vma *rmap_walk_anon_lock(struct page *page,
|
|||
if (!anon_vma)
|
||||
return NULL;
|
||||
|
||||
if (anon_vma_trylock_read(anon_vma))
|
||||
goto out;
|
||||
|
||||
if (rwc->try_lock) {
|
||||
anon_vma = NULL;
|
||||
rwc->contended = true;
|
||||
goto out;
|
||||
}
|
||||
|
||||
anon_vma_lock_read(anon_vma);
|
||||
out:
|
||||
return anon_vma;
|
||||
}
|
||||
|
||||
|
|
@ -1913,8 +1934,18 @@ static void rmap_walk_file(struct page *page, struct rmap_walk_control *rwc,
|
|||
|
||||
pgoff_start = page_to_pgoff(page);
|
||||
pgoff_end = pgoff_start + hpage_nr_pages(page) - 1;
|
||||
if (!locked)
|
||||
if (!locked) {
|
||||
if (i_mmap_trylock_read(mapping))
|
||||
goto lookup;
|
||||
|
||||
if (rwc->try_lock) {
|
||||
rwc->contended = true;
|
||||
return;
|
||||
}
|
||||
|
||||
i_mmap_lock_read(mapping);
|
||||
}
|
||||
lookup:
|
||||
vma_interval_tree_foreach(vma, &mapping->i_mmap,
|
||||
pgoff_start, pgoff_end) {
|
||||
unsigned long address = vma_address(page, vma);
|
||||
|
|
|
|||
Loading…
Reference in a new issue