linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vm_swappiness=0 should still try to avoid swapping anon memory
@ 2021-08-06 23:17 Nico Pache
  2021-08-07  1:00 ` Shakeel Butt
  0 siblings, 1 reply; 6+ messages in thread
From: Nico Pache @ 2021-08-06 23:17 UTC (permalink / raw)
  To: linux-mm, akpm, linux-kernel; +Cc: hannes, npache, aquini

Since commit b91ac374346b ("mm: vmscan: enforce inactive:active ratio at the
reclaim root") swappiness can start prematurely swapping anon memory.
This is due to the assumption that refaulting anon should always allow
the shrinker to target anon memory. Add a check for vm_swappiness being
>0 before indiscriminately targeting Anon.

Signed-off-by: Nico Pache <npache@redhat.com>
---
 mm/vmscan.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 4620df62f0ff..8b932ff72e37 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -2909,8 +2909,8 @@ static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
 
 		refaults = lruvec_page_state(target_lruvec,
 				WORKINGSET_ACTIVATE_ANON);
-		if (refaults != target_lruvec->refaults[0] ||
-			inactive_is_low(target_lruvec, LRU_INACTIVE_ANON))
+		if (vm_swappiness && (refaults != target_lruvec->refaults[0] ||
+			inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)))
 			sc->may_deactivate |= DEACTIVATE_ANON;
 		else
 			sc->may_deactivate &= ~DEACTIVATE_ANON;
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] vm_swappiness=0 should still try to avoid swapping anon memory
  2021-08-06 23:17 [PATCH] vm_swappiness=0 should still try to avoid swapping anon memory Nico Pache
@ 2021-08-07  1:00 ` Shakeel Butt
  2021-08-07  1:37   ` Nico Pache
  0 siblings, 1 reply; 6+ messages in thread
From: Shakeel Butt @ 2021-08-07  1:00 UTC (permalink / raw)
  To: Nico Pache; +Cc: Linux MM, Andrew Morton, LKML, Johannes Weiner, Rafael Aquini

On Fri, Aug 6, 2021 at 4:17 PM Nico Pache <npache@redhat.com> wrote:
>
> Since commit b91ac374346b ("mm: vmscan: enforce inactive:active ratio at the
> reclaim root") swappiness can start prematurely swapping anon memory.
> This is due to the assumption that refaulting anon should always allow
> the shrinker to target anon memory. Add a check for vm_swappiness being
> >0 before indiscriminately targeting Anon.

Did you actually observe this behavior?

>
> Signed-off-by: Nico Pache <npache@redhat.com>
> ---
>  mm/vmscan.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index 4620df62f0ff..8b932ff72e37 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -2909,8 +2909,8 @@ static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
>
>                 refaults = lruvec_page_state(target_lruvec,
>                                 WORKINGSET_ACTIVATE_ANON);
> -               if (refaults != target_lruvec->refaults[0] ||
> -                       inactive_is_low(target_lruvec, LRU_INACTIVE_ANON))
> +               if (vm_swappiness && (refaults != target_lruvec->refaults[0] ||
> +                       inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)))

If you are really seeing the said behavior then why will this fix it.
This is just about deactivating active anon LRU. I would rather look
at get_scan_count() to check why swappiness = 0 is still letting the
kernel to scan anon LRU. BTW in cgroup v1, the memcg can overwrite
their swappiness which will be preferred over system vm_swappiness.
Did you set system level swappiness or memcg one?

>                         sc->may_deactivate |= DEACTIVATE_ANON;
>                 else
>                         sc->may_deactivate &= ~DEACTIVATE_ANON;
> --
> 2.31.1
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vm_swappiness=0 should still try to avoid swapping anon memory
  2021-08-07  1:00 ` Shakeel Butt
@ 2021-08-07  1:37   ` Nico Pache
  2021-08-07  5:23     ` Shakeel Butt
  0 siblings, 1 reply; 6+ messages in thread
From: Nico Pache @ 2021-08-07  1:37 UTC (permalink / raw)
  To: Shakeel Butt
  Cc: Linux MM, Andrew Morton, LKML, Johannes Weiner, Rafael Aquini, longman


On 8/6/21 9:00 PM, Shakeel Butt wrote:
> On Fri, Aug 6, 2021 at 4:17 PM Nico Pache <npache@redhat.com> wrote:
>> Since commit b91ac374346b ("mm: vmscan: enforce inactive:active ratio at the
>> reclaim root") swappiness can start prematurely swapping anon memory.
>> This is due to the assumption that refaulting anon should always allow
>> the shrinker to target anon memory. Add a check for vm_swappiness being
>>> 0 before indiscriminately targeting Anon.
> Did you actually observe this behavior?
Yes, and I've successfully tested this patch. It does solve the issue.
>
>> Signed-off-by: Nico Pache <npache@redhat.com>
>> ---
>>  mm/vmscan.c | 4 ++--
>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/mm/vmscan.c b/mm/vmscan.c
>> index 4620df62f0ff..8b932ff72e37 100644
>> --- a/mm/vmscan.c
>> +++ b/mm/vmscan.c
>> @@ -2909,8 +2909,8 @@ static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
>>
>>                 refaults = lruvec_page_state(target_lruvec,
>>                                 WORKINGSET_ACTIVATE_ANON);
>> -               if (refaults != target_lruvec->refaults[0] ||
>> -                       inactive_is_low(target_lruvec, LRU_INACTIVE_ANON))
>> +               if (vm_swappiness && (refaults != target_lruvec->refaults[0] ||
>> +                       inactive_is_low(target_lruvec, LRU_INACTIVE_ANON)))
> If you are really seeing the said behavior then why will this fix it.
> This is just about deactivating active anon LRU. I would rather look
> at get_scan_count() to check why swappiness = 0 is still letting the
> kernel to scan anon LRU. BTW in cgroup v1, the memcg can overwrite
> their swappiness which will be preferred over system vm_swappiness.
> Did you set system level swappiness or memcg one?

This fixes the issue because shrink_list() uses the may_deactivate field to determine if it should shrink the active list. This is not the only place that can cause the may_deactivate to deactivate anon, but it is the common path of kswapd/balance_pgdat. I can look into a get_scan_count() solution however this line is the ultimate cause of telling scan controller to go for anon so i figured this is the best spot ( stop the problem at the root, not all the way down in the call path). The get_scan_count balance can also be further modified after some shrinking occurs in shrink_lruvec. 

This is only the system level swappiness. As far as cgroups, I will also take a look into that to make sure we can generalize the solution for that as well. I dont think it should be too hard. 


Thanks for the review!

-- Nico


>>                         sc->may_deactivate |= DEACTIVATE_ANON;
>>                 else
>>                         sc->may_deactivate &= ~DEACTIVATE_ANON;
>> --
>> 2.31.1
>>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vm_swappiness=0 should still try to avoid swapping anon memory
  2021-08-07  1:37   ` Nico Pache
@ 2021-08-07  5:23     ` Shakeel Butt
  2021-08-09 11:49       ` Michal Hocko
  2021-08-09 22:31       ` Nico Pache
  0 siblings, 2 replies; 6+ messages in thread
From: Shakeel Butt @ 2021-08-07  5:23 UTC (permalink / raw)
  To: Nico Pache
  Cc: Linux MM, Andrew Morton, LKML, Johannes Weiner, Rafael Aquini,
	Waiman Long

On Fri, Aug 6, 2021 at 6:37 PM Nico Pache <npache@redhat.com> wrote:
>
>
> On 8/6/21 9:00 PM, Shakeel Butt wrote:
[...]
> > If you are really seeing the said behavior then why will this fix it.
> > This is just about deactivating active anon LRU. I would rather look
> > at get_scan_count() to check why swappiness = 0 is still letting the
> > kernel to scan anon LRU. BTW in cgroup v1, the memcg can overwrite
> > their swappiness which will be preferred over system vm_swappiness.
> > Did you set system level swappiness or memcg one?
>
> This fixes the issue because shrink_list() uses the may_deactivate field
> to determine if it should shrink the active list.

First, the shrink_list() will not be called for anon LRU if get_scan_count()
has decided to not scan the anon LRU.

Second, I would like to get your attention to the following comment in
get_scan_count():

"Global reclaim will swap to prevent OOM even with no swappiness"

It seems like the behavior you are seeing is actually working as intended.
You may decide to change that behavior but you will need to motivate the
change.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vm_swappiness=0 should still try to avoid swapping anon memory
  2021-08-07  5:23     ` Shakeel Butt
@ 2021-08-09 11:49       ` Michal Hocko
  2021-08-09 22:31       ` Nico Pache
  1 sibling, 0 replies; 6+ messages in thread
From: Michal Hocko @ 2021-08-09 11:49 UTC (permalink / raw)
  To: Shakeel Butt
  Cc: Nico Pache, Linux MM, Andrew Morton, LKML, Johannes Weiner,
	Rafael Aquini, Waiman Long

On Fri 06-08-21 22:23:48, Shakeel Butt wrote:
> On Fri, Aug 6, 2021 at 6:37 PM Nico Pache <npache@redhat.com> wrote:
> >
> >
> > On 8/6/21 9:00 PM, Shakeel Butt wrote:
> [...]
> > > If you are really seeing the said behavior then why will this fix it.
> > > This is just about deactivating active anon LRU. I would rather look
> > > at get_scan_count() to check why swappiness = 0 is still letting the
> > > kernel to scan anon LRU. BTW in cgroup v1, the memcg can overwrite
> > > their swappiness which will be preferred over system vm_swappiness.
> > > Did you set system level swappiness or memcg one?
> >
> > This fixes the issue because shrink_list() uses the may_deactivate field
> > to determine if it should shrink the active list.
> 
> First, the shrink_list() will not be called for anon LRU if get_scan_count()
> has decided to not scan the anon LRU.
> 
> Second, I would like to get your attention to the following comment in
> get_scan_count():
> 
> "Global reclaim will swap to prevent OOM even with no swappiness"
> 
> It seems like the behavior you are seeing is actually working as intended.
> You may decide to change that behavior but you will need to motivate the
> change.

Yes this is true. Only the memcg has a strict no swapping behavior
historically. I do agree that the patch should go into much more details
about the existing problem though. In this context it would be really
good to explain why trashing over page cache is a better outcome than
swapping out some pages.

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vm_swappiness=0 should still try to avoid swapping anon memory
  2021-08-07  5:23     ` Shakeel Butt
  2021-08-09 11:49       ` Michal Hocko
@ 2021-08-09 22:31       ` Nico Pache
  1 sibling, 0 replies; 6+ messages in thread
From: Nico Pache @ 2021-08-09 22:31 UTC (permalink / raw)
  To: Shakeel Butt
  Cc: Linux MM, Andrew Morton, LKML, Johannes Weiner, Rafael Aquini,
	Waiman Long, mhocko


> First, the shrink_list() will not be called for anon LRU if get_scan_count()
> has decided to not scan the anon LRU.

get_scan_count() will decide to scan the anon LRU if(sc->is_file_tiny) which is set in shrink_node().

 In shrink_node() the MAY_DEACTIVATE/DEACTIVATE_ANON allows this the be activated.

> Second, I would like to get your attention to the following comment in
> get_scan_count():
>
> "Global reclaim will swap to prevent OOM even with no swappiness"

AFAIK my patchset doesn't prevent any of the OOM cases. It only prevents the anon workingset refaults

from challenging the anon if swappiness=0. 

> It seems like the behavior you are seeing is actually working as intended.
> You may decide to change that behavior but you will need to motivate the
> change.

My V3 has a lot more in the commit log. Hopefully it will clear up my motivation. I will post that now.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-08-09 22:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-06 23:17 [PATCH] vm_swappiness=0 should still try to avoid swapping anon memory Nico Pache
2021-08-07  1:00 ` Shakeel Butt
2021-08-07  1:37   ` Nico Pache
2021-08-07  5:23     ` Shakeel Butt
2021-08-09 11:49       ` Michal Hocko
2021-08-09 22:31       ` Nico Pache

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).