Received: by 2002:ac0:b08d:0:0:0:0:0 with SMTP id l13csp1723093imc; Fri, 22 Feb 2019 09:59:33 -0800 (PST) X-Google-Smtp-Source: AHgI3IagcWYq9Z148JSvFuGOe4YAvmYjROUnQiQmYtkMbo63FOa5V4LtOK6iPn0OzYu5oegl3dIf X-Received: by 2002:a63:dc54:: with SMTP id f20mr5153440pgj.410.1550858373652; Fri, 22 Feb 2019 09:59:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550858373; cv=none; d=google.com; s=arc-20160816; b=fS1D6QMHI9sXeyxOlrFfZDRDLb7mY+/ow3abJA0zA7MLL9IkuG5eEZggLvPyFjV6Yi Fz/TzvtuVM8V1vUPGmvj+hklItYTnzGqY81R74InVzFX9pjgZu6MJB6QFX8S1Cs97kDH 16D2jf9A9eFizvcVp2xdmCfMsOhoXlF9CZH5k9RHbfo46Io6p0oSGIocU869bzSR2jMA NHZ3VrnezkQ4EPleto6kNSfmolWQ0u9UXt4sWznvSayWmcrHmrg5cbGKqaafAubf2HkM 3SrRqf18YGeQhQWnHGsJfvdAcvyUontGgasgC2x+S9zL+7uaRxWC8vnh3ZYpeiaxMi7y qdBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=FXrvPnE28ZCya0GQns11qr6NhzRedm1tJTE9BWjd9QM=; b=seRhud9/Qzz0GRgwkTmJKZaj1DSsbWvHjbrqKIONuqxKTyM8pAm+Rd1YQWxjsB6vQ2 /1ejSNIpRfhdcPpVa3yjcPv17jxp8R3cpA6eg1E6am46ZYtTX7GVdoWwe5urAfv/Hlzo CyGk1BIcR6Gt7GpkaPidP1qWQO/kZxEV8M8seaqHQ6uxu7doS92VVZvHjJKjr6U8CQmT 12Z58hYy8KxwOpRDz5gzDYxv3N5yCuR1w0gevt6rw6fLA7OGK9y+2JVmniRUczGvtOIO p/NjPBER3LkyoXeUG//u6jQg825wq3e64nQoF9O0FFaWQVxBEognC39/JYaybJlKD3u6 VcJw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t5si1874740pgm.79.2019.02.22.09.59.18; Fri, 22 Feb 2019 09:59:33 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727441AbfBVR6Y (ORCPT + 99 others); Fri, 22 Feb 2019 12:58:24 -0500 Received: from relay.sw.ru ([185.231.240.75]:60204 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725832AbfBVR6Y (ORCPT ); Fri, 22 Feb 2019 12:58:24 -0500 Received: from [172.16.25.12] (helo=i7.sw.ru) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1gxF5W-000152-An; Fri, 22 Feb 2019 20:58:18 +0300 From: Andrey Ryabinin To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Ryabinin , Johannes Weiner , Michal Hocko , Vlastimil Babka , Rik van Riel , Mel Gorman , Roman Gushchin , Shakeel Butt Subject: [PATCH RFC] mm/vmscan: try to protect active working set of cgroup from reclaim. Date: Fri, 22 Feb 2019 20:58:25 +0300 Message-Id: <20190222175825.18657-1-aryabinin@virtuozzo.com> X-Mailer: git-send-email 2.19.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In a presence of more than 1 memory cgroup in the system our reclaim logic is just suck. When we hit memory limit (global or a limit on cgroup with subgroups) we reclaim some memory from all cgroups. This is sucks because, the cgroup that allocates more often always wins. E.g. job that allocates a lot of clean rarely used page cache will push out of memory other jobs with active relatively small all in memory working set. To prevent such situations we have memcg controls like low/max, etc which are supposed to protect jobs or limit them so they to not hurt others. But memory cgroups are very hard to configure right because it requires precise knowledge of the workload which may vary during the execution. E.g. setting memory limit means that job won't be able to use all memory in the system for page cache even if the rest the system is idle. Basically our current scheme requires to configure every single cgroup in the system. I think we can do better. The idea proposed by this patch is to reclaim only inactive pages and only from cgroups that have big (!inactive_is_low()) inactive list. And go back to shrinking active lists only if all inactive lists are low. Now, the simple test case to demonstrate the effect of the patch. The job in one memcg repeatedly compresses one file: perf stat -n --repeat 20 gzip -ck sample > /dev/null and just 'dd' running in parallel reading the disk in another cgroup. Before: Performance counter stats for 'gzip -ck sample' (20 runs): 17.673572290 seconds time elapsed ( +- 5.60% ) After: Performance counter stats for 'gzip -ck sample' (20 runs): 11.426193980 seconds time elapsed ( +- 0.20% ) The more often dd cgroup allocates memory, the more gzip suffer. With 4 parallel dd instead of one: Before: Performance counter stats for 'gzip -ck sample' (20 runs): 499.976782013 seconds time elapsed ( +- 23.13% ) After: Performance counter stats for 'gzip -ck sample' (20 runs): 11.307450516 seconds time elapsed ( +- 0.27% ) It would be possible to achieve the similar effect by setting the memory.low on gzip cgroup, but the best value for memory.low depends on the size of the 'sample' file. It also possible to limit the 'dd' job, but just imagine something more sophisticated than just 'dd', the job that would benefit from occupying all available memory. The best limit for such job would be something like 'total_memory' - 'sample size' which is again unknown. Signed-off-by: Andrey Ryabinin Cc: Johannes Weiner Cc: Michal Hocko Cc: Vlastimil Babka Cc: Rik van Riel Cc: Mel Gorman Cc: Roman Gushchin Cc: Shakeel Butt --- mm/vmscan.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index efd10d6b9510..2f562c3358ab 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -104,6 +104,8 @@ struct scan_control { /* One of the zones is ready for compaction */ unsigned int compaction_ready:1; + unsigned int may_shrink_active:1; + /* Allocation order */ s8 order; @@ -2489,6 +2491,10 @@ static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, scan >>= sc->priority; + if (!sc->may_shrink_active && inactive_list_is_low(lruvec, + file, memcg, sc, false)) + scan = 0; + /* * If the cgroup's already been deleted, make sure to * scrape out the remaining cache. @@ -2733,6 +2739,7 @@ static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) struct reclaim_state *reclaim_state = current->reclaim_state; unsigned long nr_reclaimed, nr_scanned; bool reclaimable = false; + bool retry; do { struct mem_cgroup *root = sc->target_mem_cgroup; @@ -2742,6 +2749,8 @@ static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) }; struct mem_cgroup *memcg; + retry = false; + memset(&sc->nr, 0, sizeof(sc->nr)); nr_reclaimed = sc->nr_reclaimed; @@ -2813,6 +2822,13 @@ static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) } } while ((memcg = mem_cgroup_iter(root, memcg, &reclaim))); + if ((sc->nr_scanned - nr_scanned) == 0 && + !sc->may_shrink_active) { + sc->may_shrink_active = 1; + retry = true; + continue; + } + if (reclaim_state) { sc->nr_reclaimed += reclaim_state->reclaimed_slab; reclaim_state->reclaimed_slab = 0; @@ -2887,7 +2903,7 @@ static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) current_may_throttle() && pgdat_memcg_congested(pgdat, root)) wait_iff_congested(BLK_RW_ASYNC, HZ/10); - } while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, + } while (retry || should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed, sc->nr_scanned - nr_scanned, sc)); /* -- 2.19.2