From 63550e808ee52ee3e61321bbfa2128e44dcc7bee Mon Sep 17 00:00:00 2001
Date: Thu, 11 Jul 2013 18:04:25 +0800
Subject: [PATCH 02/15] memcg: add a sysctl memcg_target_reclaim for memcg reclaim purpose
This is prepare patch for memcg target reclaim. The default value
of the sysctl is 0.

Signed-off-by: Sha Zhengju <handai.szj@taobao.com>
From 520f3c381a5756191d5b705cbfa963c6ec921639 Mon Sep 17 00:00:00 2001
Date: Thu, 18 Jul 2013 12:59:11 +0800
Subject: [PATCH 03/15] memcg: Only reclaim sub-memcg that over softlimit while doing target reclaim
Now softlimit is only considered in global kswapd reclaim, both the
global direct and memcg target reclaim is ignoring it. But considering
the following use case in Aliyun environment:

        root (use_hierarchy = 1)
          - sshd
          - ...
          - tubo (with hardlimit)
                - chunkserver (with softlimit & hardlimit)
                - work2       (with softlimit & hardlimit)
                - ...         (with softlimit & hardlimit)

Memcg chunkserver uses most of the memory as page cache but other memcgs like
work2 only use a few. Both of them are bound with softlimit and hardlimit.
If memory reclaim is going on, we hope chunkserver can be squeezed most and
relieve memcg like work2 since we don't want to reclaim its little memory.

For global kswapd reclaim, we can do that by setting artful softlimit. A rb-tree
is used to track memcgs that consume most with regard to its softlimit and the
reclaimer will try to push them back to their softlimits. But it doesn't work for
memcg target reclaim - that the memory contention is triggered from tubo cgroup.
In such a case, every sub-memcg will be squeezed equally.

In this patch, we add a new function to deal with target reclaim. It is based on
memcg hierarchy traversing and is independent of the softlimit rb-tree. First we
scan all of the sub-memcgs to find the one beyond softlimit most, and then start
reclaiming. If there is no victim memcg or nothing is reclaimed for two rounds,
we fall back to original mem_cgroup_hierarchical_reclaim().

This feature is controlled by sysctl memcg_target_reclaim.

Signed-off-by: Sha Zhengju <handai.szj@taobao.com>