2014-07-31 11:50:05

by Jerome Marchand

[permalink] [raw]
Subject: [PATCH 0/2] Fix excessive swapping when memcg are enabled

When memory cgroups are enabled, reclaim code may force scan of
anonymous page in one memcg even when there are plenty of file pages
in other memcg. It has lead to excessive swapping in a real life
example: a virtual machine running in a memcg while there is
background I/O.

The first patch just updates an outdated comment that has bugged me
for a while but that I never bothered to update. The second patch
actually fixes the issue.

Jerome Marchand (2):
mm, vmscan: fix an outdated comment still mentioning get_scan_ratio
memcg, vmscan: Fix forced anon scan

mm/vmscan.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

--
1.9.3


2014-07-31 11:50:09

by Jerome Marchand

[permalink] [raw]
Subject: [PATCH 1/2] mm, vmscan: fix an outdated comment still mentioning get_scan_ratio

Quite a while ago, get_scan_ratio() has been renamed get_scan_count(),
however a comment in shrink_active_list() still mention it. This patch
fixes the outdated comment.

Signed-off-by: Jerome Marchand <[email protected]>
---
mm/vmscan.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index d698f4f..079918d 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1759,7 +1759,7 @@ static void shrink_active_list(unsigned long nr_to_scan,
* Count referenced pages from currently used mappings as rotated,
* even though only some of them are actually re-activated. This
* helps balance scan pressure between file and anonymous pages in
- * get_scan_ratio.
+ * get_scan_count.
*/
reclaim_stat->recent_rotated[file] += nr_rotated;

--
1.9.3

2014-07-31 11:50:48

by Jerome Marchand

[permalink] [raw]
Subject: [PATCH 2/2] memcg, vmscan: Fix forced scan of anonymous pages

When memory cgoups are enabled, the code that decides to force to scan
anonymous pages in get_scan_count() compares global values (free,
high_watermark) to a value that is restricted to a memory cgroup
(file). It make the code over-eager to force anon scan.

For instance, it will force anon scan when scanning a memcg that is
mainly populated by anonymous page, even when there is plenty of file
pages to get rid of in others memcgs, even when swappiness == 0. It
breaks user's expectation about swappiness and hurts performance.

This patch make sure that forced anon scan only happens when there not
enough file pages for the all zone, not just in one random memcg.

Signed-off-by: Jerome Marchand <[email protected]>
---
mm/vmscan.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/mm/vmscan.c b/mm/vmscan.c
index 079918d..3ad2069 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1950,8 +1950,11 @@ static void get_scan_count(struct lruvec *lruvec, int swappiness,
*/
if (global_reclaim(sc)) {
unsigned long free = zone_page_state(zone, NR_FREE_PAGES);
+ unsigned long zonefile =
+ zone_page_state(zone, NR_LRU_BASE + LRU_ACTIVE_FILE) +
+ zone_page_state(zone, NR_LRU_BASE + LRU_INACTIVE_FILE);

- if (unlikely(file + free <= high_wmark_pages(zone))) {
+ if (unlikely(zonefile + free <= high_wmark_pages(zone))) {
scan_balance = SCAN_ANON;
goto out;
}
--
1.9.3

2014-07-31 12:30:28

by Michal Hocko

[permalink] [raw]
Subject: Re: [PATCH 2/2] memcg, vmscan: Fix forced scan of anonymous pages

On Thu 31-07-14 13:49:45, Jerome Marchand wrote:
> When memory cgoups are enabled, the code that decides to force to scan
> anonymous pages in get_scan_count() compares global values (free,
> high_watermark) to a value that is restricted to a memory cgroup
> (file). It make the code over-eager to force anon scan.

OK, I though this was about memcg reclaim according to the subject but
this is in fact global reclaim when there are multiple memcgs present.
Good. You are right that apples are compared to oranges here.

> For instance, it will force anon scan when scanning a memcg that is
> mainly populated by anonymous page, even when there is plenty of file
> pages to get rid of in others memcgs, even when swappiness == 0. It
> breaks user's expectation about swappiness and hurts performance.
>
> This patch make sure that forced anon scan only happens when there not
> enough file pages for the all zone, not just in one random memcg.

OK, makes sense to me.

> Signed-off-by: Jerome Marchand <[email protected]>

Although I have never seen this before I can imagine specialized memcgs
running with mostly anon memory so I would even consider it a stable
material.

Acked-by: Michal Hocko <[email protected]>

> ---
> mm/vmscan.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index 079918d..3ad2069 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -1950,8 +1950,11 @@ static void get_scan_count(struct lruvec *lruvec, int swappiness,
> */
> if (global_reclaim(sc)) {
> unsigned long free = zone_page_state(zone, NR_FREE_PAGES);
> + unsigned long zonefile =
> + zone_page_state(zone, NR_LRU_BASE + LRU_ACTIVE_FILE) +
> + zone_page_state(zone, NR_LRU_BASE + LRU_INACTIVE_FILE);
>
> - if (unlikely(file + free <= high_wmark_pages(zone))) {
> + if (unlikely(zonefile + free <= high_wmark_pages(zone))) {
> scan_balance = SCAN_ANON;
> goto out;
> }

You could move file and anon further down when we actually use them.
--
Michal Hocko
SUSE Labs

2014-07-31 15:07:06

by Johannes Weiner

[permalink] [raw]
Subject: Re: [PATCH 2/2] memcg, vmscan: Fix forced scan of anonymous pages

On Thu, Jul 31, 2014 at 01:49:45PM +0200, Jerome Marchand wrote:
> When memory cgoups are enabled, the code that decides to force to scan
> anonymous pages in get_scan_count() compares global values (free,
> high_watermark) to a value that is restricted to a memory cgroup
> (file). It make the code over-eager to force anon scan.
>
> For instance, it will force anon scan when scanning a memcg that is
> mainly populated by anonymous page, even when there is plenty of file
> pages to get rid of in others memcgs, even when swappiness == 0. It
> breaks user's expectation about swappiness and hurts performance.
>
> This patch make sure that forced anon scan only happens when there not
> enough file pages for the all zone, not just in one random memcg.
>
> Signed-off-by: Jerome Marchand <[email protected]>

Acked-by: Johannes Weiner <[email protected]>

2014-07-31 15:39:35

by Rik van Riel

[permalink] [raw]
Subject: Re: [PATCH 2/2] memcg, vmscan: Fix forced scan of anonymous pages

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 07/31/2014 07:49 AM, Jerome Marchand wrote:
> When memory cgoups are enabled, the code that decides to force to
> scan anonymous pages in get_scan_count() compares global values
> (free, high_watermark) to a value that is restricted to a memory
> cgroup (file). It make the code over-eager to force anon scan.
>
> For instance, it will force anon scan when scanning a memcg that
> is mainly populated by anonymous page, even when there is plenty of
> file pages to get rid of in others memcgs, even when swappiness ==
> 0. It breaks user's expectation about swappiness and hurts
> performance.
>
> This patch make sure that forced anon scan only happens when there
> not enough file pages for the all zone, not just in one random
> memcg.
>
> Signed-off-by: Jerome Marchand <[email protected]>

That fix is a lot smaller than I thought it would be. Nice.

Reviewed-by: Rik van Riel <[email protected]>

- --
All rights reversed
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQEcBAEBAgAGBQJT2mL7AAoJEM553pKExN6DbzsH/ArKqWXYFfz7/hjADJXz85aK
ygWdjpK18MbFeUMW3nL324j2567TXWpC2G7SgxSPjYnF/qvKjpoQHJk7WvisymjE
p+5jGQAxzXgjlq0usGoFRrWUnR6vkdjTx0K8r6MO/asMLbvDBjkXvaURHdcV6fx4
nUbkF/GRXGAGcnHOEks294w+8j8R50bugnX+IfmKo73eteNcMWU7Ga+b93kUmz3p
4EE2PRpRKFWtpTAhpFlFI46gfu+e7I1Ziu2pzNUlYOP3P7t9pRS8YOI5JNOnyDfi
lrbOXzoSqs6sbIlDd//A/p7u6Pzr+HnpbaxCrf9UCdNaMMqvb0gDQWv7221gI24=
=BfHz
-----END PGP SIGNATURE-----