mm: drop lruvec->lru_lock if contended when skipping folio
Subsystems (3)
PATCH RESEND 1/1 mm: drop lruvec->lru_lock if contended when skipping folio
lruvec->lru_lock is highly contended and is held when calling isolate_lru_folios. If the lru has a large number of CMA folios consecutively, while the allocation type requested is not MIGRATE_MOVABLE, isolate_lru_folios can hold the lock for a very long time while it skips those. vmscan_lru_isolate tracepoint showed that skipped can go above 70k in production and lockstat shows that waittime-max is x1000 higher without this patch.
This can cause lockups [1] and high memory pressure for extended periods of time [2]. Hence release the lock if its contended when skipping a folio to give other tasks a chance to acquire it and not stall.
[1] https://lore.kernel.org/all/CAOUHufbkhMZYz20aM_3rHZ3OcK4m2puji2FGpUpn_-DevGk3Kg@mail.gmail.com/
[2] https://lore.kernel.org/all/[email protected]/
--- mm/vmscan.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
mm/vmscan.c +8 -2
@@ -1695,8 +1695,14 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan,if (folio_zonenum (folio) > sc->reclaim_idx ||skip_cma (folio, sc)) { nr_skipped[folio_zonenum (folio)] += nr_pages; - move_to = &folios_skipped; - goto move; +list_move (&folio->lru, &folios_skipped); +if (!spin_is_contended (&lruvec->lru_lock)) +continue ; +if (!list_empty (dst)) +break ; +spin_unlock_irq (&lruvec->lru_lock); +cond_resched (); +spin_lock_irq (&lruvec->lru_lock); }/*
Signature · 1 line
2.43.5
Andrew Morton
maintainerQuoted 20 lines · On Mon, 19 Aug 2024 19:46:48 +0100 Usama Arif <[email protected]> wrote:
lruvec->lru_lockis highly contended and is held when callingisolate_lru_folios. If the lru has a large number of CMA folios consecutively, while the allocation type requested is notMIGRATE_MOVABLE,isolate_lru_folioscan hold the lock for a very long time while it skips those.vmscan_lru_isolatetracepoint showed that skipped can go above 70k in production and lockstat shows that waittime-max is x1000 higher without this patch.
This can cause lockups [1] and high memory pressure for extended periods of time [2]. Hence release the lock if its contended when skipping a folio to give other tasks a chance to acquire it and not stall....
mm/vmscan.c +0
-1@@ -1695,8 +1695,14 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan,if (folio_zonenum (folio) > sc->reclaim_idx ||skip_cma (folio, sc)) { nr_skipped[folio_zonenum (folio)] += nr_pages; - move_to = &folios_skipped;
mm/vmscan.c static unsigned long isolate_lru_folios(unsigned long nr_to_scan, · line 1706
Quoted 10 lines
mm/vmscan.c +8
-1- goto move; +list_move (&folio->lru, &folios_skipped); +if (!spin_is_contended (&lruvec->lru_lock)) +continue ; +if (!list_empty (dst)) +break ; +spin_unlock_irq (&lruvec->lru_lock); +cond_resched (); +spin_lock_irq (&lruvec->lru_lock); }
Oh geeze ugly thing. Must we do this?
The games that function plays with src, dst and move_to are a bit hard to follow. Some tasteful comments explaining what's going on would help.
Also that test of !list_empty(dst). It would be helpful to comment the dynamics which are happening in this case - why we're testing dst here.
Quoted 35 lines · Andrew Morton
Quoted 20 lines · On Mon, 19 Aug 2024 19:46:48 +0100 Usama Arif <[email protected]> wrote:
lruvec->lru_lockis highly contended and is held when callingisolate_lru_folios. If the lru has a large number of CMA folios consecutively, while the allocation type requested is notMIGRATE_MOVABLE,isolate_lru_folioscan hold the lock for a very long time while it skips those.vmscan_lru_isolatetracepoint showed that skipped can go above 70k in production and lockstat shows that waittime-max is x1000 higher without this patch.
This can cause lockups [1] and high memory pressure for extended periods of time [2]. Hence release the lock if its contended when skipping a folio to give other tasks a chance to acquire it and not stall....
mm/vmscan.c +0
-1@@ -1695,8 +1695,14 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan,if (folio_zonenum (folio) > sc->reclaim_idx ||skip_cma (folio, sc)) { nr_skipped[folio_zonenum (folio)] += nr_pages; - move_to = &folios_skipped;mm/vmscan.c static unsigned long isolate_lru_folios(unsigned long nr_to_scan, · line 1706
Quoted 10 lines
mm/vmscan.c +8
-1- goto move; +list_move (&folio->lru, &folios_skipped); +if (!spin_is_contended (&lruvec->lru_lock)) +continue ; +if (!list_empty (dst)) +break ; +spin_unlock_irq (&lruvec->lru_lock); +cond_resched (); +spin_lock_irq (&lruvec->lru_lock); }Oh geeze ugly thing. Must we do this?
Quoted 8 lines
The games that function plays with src, dst and
move_toare a bit hard to follow. Some tasteful comments explaining what's going on would help.Also that test of !list_empty(dst). It would be helpful to comment the dynamics which are happening in this case - why we're testing dst here.
So Johannes pointed out to me that this is not going to properly fix the problem of holding the lru_lock for a long time introduced in [1] because of 2 reasons:
- the task that is doing lock break is hoarding folios on folios_skipped and making the lru shorter, I didn't see it in the usecase I was trying, but it could be that yielding the lock to the other task is not of much use as it is going to go through a much shorter lru list or even an empty lru list and would OOM, while the folio it is looking for is on folios_skipped. We would be substituting one OOM problem for another with this patch.
- Compaction code goes through pages by pfn and not using the list, as this patch does not clear lru flag, compaction could claim this folio.
The patch in [1] is severely breaking production at Meta and its not a proper fix to the problem that the commit was trying to be solved. It results in holding the lru_lock for a very significant amount of time, stalling all other processes trying to claim memory, creating very high memory pressure for large periods of time and causing OOM.
The way forward would be to revert it and try to come up with a longer term solution that the original commit tried to solve. If no one is opposed to it, I will wait a couple of days for comments and send a revert patch.
[1] https://lore.kernel.org/all/[email protected]/
Thanks,
Usama
Quoted 18 lines · Usama Arif
So Johannes pointed out to me that this is not going to properly fix the problem of holding the
lru_lockfor a long time introduced in [1] because of 2 reasons: - the task that is doing lock break is hoarding folios onfolios_skippedand making the lru shorter, I didn't see it in the usecase I was trying, but it could be that yielding the lock to the other task is not of much use as it is going to go through a much shorter lru list or even an empty lru list and would OOM, while the folio it is looking for is onfolios_skipped. We would be substituting one OOM problem for another with this patch. - Compaction code goes through pages by pfn and not using the list, as this patch does not clear lru flag, compaction could claim this folio.The patch in [1] is severely breaking production at Meta and its not a proper fix to the problem that the commit was trying to be solved. It results in holding the
lru_lockfor a very significant amount of time, stalling all other processes trying to claim memory, creating very high memory pressure for large periods of time and causing OOM.
Quoted 4 lines
The way forward would be to revert it and try to come up with a longer term solution that the original commit tried to solve. If no one is opposed to it, I will wait a couple of days for comments and send a revert patch.
I agree with the concern, but for a different reason. Commit 5da226dbfce3 ("mm: skip CMA pages when they are not available") was intended as an optimization, but it changed the behavior of the isolate_lru_folios() function in a way that had significant, unintended consequences.
One such consequence was a notable increase in lock contention, as described in [1]. Addressing this lock contention issue with a quick fix seems like a suboptimal solution for such a core part of the system.
Instead, a better approach would be to rethink the original optimization. Rather than applying a band-aid to the lock contention problem, it would be more prudent to revisit the changes introduced by commit 5da226dbfce3 and explore alternative optimization strategies that do not have such far-reaching and difficult-to-diagnose effects.
Quoted 43 lines · Usama Arif
Quoted 39 lines · Andrew Morton
Quoted 20 lines · On Mon, 19 Aug 2024 19:46:48 +0100 Usama Arif <[email protected]> wrote:
lruvec->lru_lockis highly contended and is held when callingisolate_lru_folios. If the lru has a large number of CMA folios consecutively, while the allocation type requested is notMIGRATE_MOVABLE,isolate_lru_folioscan hold the lock for a very long time while it skips those.vmscan_lru_isolatetracepoint showed that skipped can go above 70k in production and lockstat shows that waittime-max is x1000 higher without this patch.
This can cause lockups [1] and high memory pressure for extended periods of time [2]. Hence release the lock if its contended when skipping a folio to give other tasks a chance to acquire it and not stall....
mm/vmscan.c +0
-1@@ -1695,8 +1695,14 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan,if (folio_zonenum (folio) > sc->reclaim_idx ||skip_cma (folio, sc)) { nr_skipped[folio_zonenum (folio)] += nr_pages; - move_to = &folios_skipped;mm/vmscan.c static unsigned long isolate_lru_folios(unsigned long nr_to_scan, · line 1706
Quoted 10 lines
mm/vmscan.c +8
-1- goto move; +list_move (&folio->lru, &folios_skipped); +if (!spin_is_contended (&lruvec->lru_lock)) +continue ; +if (!list_empty (dst)) +break ; +spin_unlock_irq (&lruvec->lru_lock); +cond_resched (); +spin_lock_irq (&lruvec->lru_lock); }Oh geeze ugly thing. Must we do this?
The games that function plays with src, dst and
move_toare a bit hard to follow. Some tasteful comments explaining what's going on would help.
Quoted 7 lines
Quoted 4 lines
Also that test of !list_empty(dst). It would be helpful to comment the dynamics which are happening in this case - why we're testing dst here.
So Johannes pointed out to me that this is not going to properly fix the problem of holding the
lru_lockfor a long time introduced in [1] because of 2 reasons:
- the task that is doing lock break is hoarding folios onfolios_skippedand making the lru shorter, I didn't see it in the usecase I was trying, but it could be that yielding the lock to the other task is not of much use as it is going to go through a much shorter lru list or even an empty lru list and would OOM, while the folio it is looking for is onfolios_skipped. We would be substituting one OOM problem for another with this patch.
Other tasks can not get folios either if they can not use CMA after the reclaiming even without the original patch. I am ok with your fix patch except the 'if (!list_empty(dst))' puzzled me.
Quoted 5 lines
- Compaction code goes through pages by pfn and not using the list, as this patch does not clear lru flag, compaction could claim this folio.
The patch in [1] is severely breaking production at Meta and its not a proper fix to the problem that the commit was trying to be solved. It results in holding the
lru_lockfor a very significant amount of time, stalling all other processes trying to claim memory, creating very high memory pressure for large periods of time and causing OOM.The way forward would be to revert it and try to come up with a longer term solution that the original commit tried to solve. If no one is opposed to it, I will wait a couple of days for comments and send a revert patch.
I mainly focus on android systems which have no such scenarios as server encountered. I agree with reverting it if this fix can not be accepted.
Quoted 5 lines
[1] https://lore.kernel.org/all/[email protected]/
Thanks,
Usama
Quoted 49 lines · Zhaoyang Huang
Quoted 48 lines · Usama Arif
Quoted 43 lines · Andrew Morton
Quoted 20 lines · On Mon, 19 Aug 2024 19:46:48 +0100 Usama Arif <[email protected]> wrote:
lruvec->lru_lockis highly contended and is held when callingisolate_lru_folios. If the lru has a large number of CMA folios consecutively, while the allocation type requested is notMIGRATE_MOVABLE,isolate_lru_folioscan hold the lock for a very long time while it skips those.vmscan_lru_isolatetracepoint showed that skipped can go above 70k in production and lockstat shows that waittime-max is x1000 higher without this patch.
This can cause lockups [1] and high memory pressure for extended periods of time [2]. Hence release the lock if its contended when skipping a folio to give other tasks a chance to acquire it and not stall....
mm/vmscan.c +0
-1@@ -1695,8 +1695,14 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan,if (folio_zonenum (folio) > sc->reclaim_idx ||skip_cma (folio, sc)) { nr_skipped[folio_zonenum (folio)] += nr_pages; - move_to = &folios_skipped;mm/vmscan.c static unsigned long isolate_lru_folios(unsigned long nr_to_scan, · line 1706
Quoted 10 lines
mm/vmscan.c +8
-1- goto move; +list_move (&folio->lru, &folios_skipped); +if (!spin_is_contended (&lruvec->lru_lock)) +continue ; +if (!list_empty (dst)) +break ; +spin_unlock_irq (&lruvec->lru_lock); +cond_resched (); +spin_lock_irq (&lruvec->lru_lock); }Oh geeze ugly thing. Must we do this?
The games that function plays with src, dst and
move_toare a bit hard to follow. Some tasteful comments explaining what's going on would help.Also that test of !list_empty(dst). It would be helpful to comment the dynamics which are happening in this case - why we're testing dst here.
Quoted 5 lines
Quoted 2 lines
So Johannes pointed out to me that this is not going to properly fix the problem of holding the
lru_lockfor a long time introduced in [1] because of 2 reasons:
- the task that is doing lock break is hoarding folios onfolios_skippedand making the lru shorter, I didn't see it in the usecase I was trying, but it could be that yielding the lock to the other task is not of much use as it is going to go through a much shorter lru list or even an empty lru list and would OOM, while the folio it is looking for is onfolios_skipped. We would be substituting one OOM problem for another with this patch.Other tasks can not get folios either if they can not use CMA after the reclaiming even without the original patch. I am ok with your fix patch except the 'if (!list_empty(dst))' puzzled me.
The 'if (!list_empty(dst))' was there so that if the spinlock is contended and we are still skipping, then the shrink_active/inactive_list can still use the ones that are in destination, rather then continue skipping.
Both the original patch and this fix substitute one OOM problem for another one.
Quoted 9 lines
Quoted 5 lines
- Compaction code goes through pages by pfn and not using the list, as this patch does not clear lru flag, compaction could claim this folio.
The patch in [1] is severely breaking production at Meta and its not a proper fix to the problem that the commit was trying to be solved. It results in holding the
lru_lockfor a very significant amount of time, stalling all other processes trying to claim memory, creating very high memory pressure for large periods of time and causing OOM.The way forward would be to revert it and try to come up with a longer term solution that the original commit tried to solve. If no one is opposed to it, I will wait a couple of days for comments and send a revert patch.
I mainly focus on android systems which have no such scenarios as server encountered. I agree with reverting it if this fix can not be accepted.
Quoted 1 line
Yeah I think the 2nd point about compaction is a bigger issue, we can try fixing the 2nd point by clearing lru, but the first point will remain. I will send a revert for the original.
Thanks
Quoted 4 lines
Quoted 4 lines
[1] https://lore.kernel.org/all/[email protected]/
Thanks,
Usama
Quoted 11 lines · Usama Arif
lruvec->lru_lockis highly contended and is held when callingisolate_lru_folios. If the lru has a large number of CMA folios consecutively, while the allocation type requested is notMIGRATE_MOVABLE,isolate_lru_folioscan hold the lock for a very long time while it skips those.vmscan_lru_isolatetracepoint showed that skipped can go above 70k in production and lockstat shows that waittime-max is x1000 higher without this patch.
This can cause lockups [1] and high memory pressure for extended periods of time [2]. Hence release the lock if its contended when skipping a folio to give other tasks a chance to acquire it and not stall.
Quoted 2 lines
[1] https://lore.kernel.org/all/CAOUHufbkhMZYz20aM_3rHZ3OcK4m2puji2FGpUpn_-DevGk3Kg@mail.gmail.com/
[2] https://lore.kernel.org/all/[email protected]/
Though the above link[2] mentions it, can you explicitly include the specific condition that we saw in the patch description?
"isolate_lru_folios() can end up scanning through a huge number of folios with lruvec spinlock held. For FIO workload, ~150million order=0 folios were skipped to isolate a few ZONE_DMA folios."
Regards,
Bharata.