Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp2608143imb; Mon, 4 Mar 2019 09:18:24 -0800 (PST) X-Google-Smtp-Source: APXvYqzzdbsy7VS2qczjQaJ+fqfkgdXC6jDX9RXCQUtKFOLXfm5e4NRugrSv5D/BZdprZzWnpM7y X-Received: by 2002:a65:4284:: with SMTP id j4mr19523915pgp.334.1551719903960; Mon, 04 Mar 2019 09:18:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551719903; cv=none; d=google.com; s=arc-20160816; b=xmNyfjcmNz0ex3btzMJEXwCv0c6CmHWt+Mg5olZcr7Mc8K6FJ4JgXxlv8s8H288N6A tOPLzSmuuJWvzVnNxRYIq1hNDmK8o+OQixch+Ie62CvImPFDTuYQEbR7lXL6Jfse1kAm NVqj+l0Dr1cmF4G61KYWvzPdBzpMTfwOxYGkHMnlKR23qu/xRQIJ4Os3/zRpyWaocwSz oKHhvDBgxJ8gWAb39tiBNHOODrLU/BL0m3CQ9V2CS3pmR6rQV3D+USISXmMArEn2xsLj OLj+7kLlhxYAdv+oULLTCJaGNgyjr+8sYvrr/YP9vkJObbpe2LlhHQYGPAXfVLn6px2u qjlQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:references:cc:to:subject:from; bh=7l5cyQe6q3LjOQsG6yl1fbouBp1l3qIgGgLVNtHDt1Q=; b=lqT1LzA6AcQAH7iNDAr+yvV8gTEn2oAoj6C+318xpCqY2EQYVSQUf51bqOrXa9PzZT tZu1GeMSW3YPtryvMFSw5R8Cjw8mfrF6JOLhvsGpij8hp7IpLtyQxUP9ykBEe+W6IrlN PwDTvHWxLFOYuTR/ZBzP2uTaaZFbXg+2ryoVHNKjEnC902Kje5t902PppvX02mYLj4Bc KDrMbIp8mwToZhwaHvsCNo6XeMxKep74nUW4er3XOJz6en6Gmw8209MWHbURfJ35fWRy IH2wdoHJVwZ4kGvo/IELOLTqu7EH2OQ54iuNbDM71MjGBn7YGOUfWVJPlzGXYZfl/TWB DTtg== 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 p4si5730288pfi.93.2019.03.04.09.18.09; Mon, 04 Mar 2019 09:18:23 -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 S1727045AbfCDRCk (ORCPT + 99 others); Mon, 4 Mar 2019 12:02:40 -0500 Received: from relay.sw.ru ([185.231.240.75]:43810 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726098AbfCDRCk (ORCPT ); Mon, 4 Mar 2019 12:02:40 -0500 Received: from [172.16.25.12] by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1h0qyg-0000MJ-7U; Mon, 04 Mar 2019 20:02:10 +0300 From: Andrey Ryabinin Subject: Re: [PATCH RFC] mm/vmscan: try to protect active working set of cgroup from reclaim. To: Johannes Weiner Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Michal Hocko , Vlastimil Babka , Rik van Riel , Mel Gorman , Roman Gushchin , Shakeel Butt References: <20190222175825.18657-1-aryabinin@virtuozzo.com> <20190222191552.GA15922@cmpxchg.org> <7c915942-6f52-e7a4-b879-e4c99dd65968@virtuozzo.com> <20190301174907.GA2375@cmpxchg.org> <51ac7aaa-6890-c674-854d-1e2d132b83f9@virtuozzo.com> <20190301222010.GA9215@cmpxchg.org> Message-ID: Date: Mon, 4 Mar 2019 20:02:27 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.2 MIME-Version: 1.0 In-Reply-To: <20190301222010.GA9215@cmpxchg.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/2/19 1:20 AM, Johannes Weiner wrote: > On Fri, Mar 01, 2019 at 10:46:34PM +0300, Andrey Ryabinin wrote: >> On 3/1/19 8:49 PM, Johannes Weiner wrote: >>> On Fri, Mar 01, 2019 at 01:38:26PM +0300, Andrey Ryabinin wrote: >>>> On 2/26/19 3:50 PM, Andrey Ryabinin wrote: >>>>> On 2/22/19 10:15 PM, Johannes Weiner wrote: >>>>>> On Fri, Feb 22, 2019 at 08:58:25PM +0300, Andrey Ryabinin wrote: >>>>>>> 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. >>>>>> >>>>>> Yes, you are absolutely right. >>>>>> >>>>>> We shouldn't go after active pages as long as there are plenty of >>>>>> inactive pages around. That's the global reclaim policy, and we >>>>>> currently fail to translate that well to cgrouped systems. >>>>>> >>>>>> Setting group protections or limits would work around this problem, >>>>>> but they're kind of a red herring. We shouldn't ever allow use-once >>>>>> streams to push out hot workingsets, that's a bug. >>>>>> >>>>>>> @@ -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; >>>>>>> + } >>>>>> >>>>>> Using !scanned as the gate could be a problem. There might be a cgroup >>>>>> that has inactive pages on the local level, but when viewed from the >>>>>> system level the total inactive pages in the system might still be low >>>>>> compared to active ones. In that case we should go after active pages. >>>>>> >>>>>> Basically, during global reclaim, the answer for whether active pages >>>>>> should be scanned or not should be the same regardless of whether the >>>>>> memory is all global or whether it's spread out between cgroups. >>>>>> >>>>>> The reason this isn't the case is because we're checking the ratio at >>>>>> the lruvec level - which is the highest level (and identical to the >>>>>> node counters) when memory is global, but it's at the lowest level >>>>>> when memory is cgrouped. >>>>>> >>>>>> So IMO what we should do is: >>>>>> >>>>>> - At the beginning of global reclaim, use node_page_state() to compare >>>>>> the INACTIVE_FILE:ACTIVE_FILE ratio and then decide whether reclaim >>>>>> can go after active pages or not. Regardless of what the ratio is in >>>>>> individual lruvecs. >>>>>> >>>>>> - And likewise at the beginning of cgroup limit reclaim, walk the >>>>>> subtree starting at sc->target_mem_cgroup, sum up the INACTIVE_FILE >>>>>> and ACTIVE_FILE counters, and make inactive_is_low() decision on >>>>>> those sums. >>>>>> >>>>> >>>>> Sounds reasonable. >>>>> >>>> >>>> On the second thought it seems to be better to keep the decision on lru level. >>>> There are couple reasons for this: >>>> >>>> 1) Using bare node_page_state() (or sc->targe_mem_cgroup's total_[in]active counters) would be wrong. >>>> Because some cgroups might have protection set (memory.low) and we must take it into account. Also different >>>> cgroups have different available swap space/memory.swappiness and it must be taken into account as well to. >>>> >>>> So it has to be yet another full memcg-tree iteration. >>> >>> It should be possible to take that into account on the first iteration >>> and adjust the inactive/active counters in proportion to how much of >>> the cgroup's total memory is exempt by memory.low or min, right? >>> >> >> Should be possible, more complexity though to this subtle code. >> >> >>>> 2) Let's consider simple case. Two cgroups, one with big 'active' set of pages the other allocates one-time used pages. >>>> So the total inactive is low, thus checking inactive ratio on higher level will result in reclaiming pages. >>>> While with check on lru-level only inactive will be reclaimed. >>> >>> It's the other way around. Let's say you have two cgroups, A and B: >>> >>> A: 500M inactive 10G active -> inactive is low >>> B: 10G inactive 500M active -> inactive is NOT low >>> ---------------------------------------------------------- >>> global: 10.5G inactive 10.5G active -> inactive is NOT low >>> >>> Checking locally will scan active pages from A. >> >> No, checking locally will not scan active from A. Initial state of >> sc->may_shrink_active = 0, so A group will be skipped completely, >> and will reclaim from B. Since overall reclaim was successful, >> sc->may_shrink_active remain 0 and A will be protected as long as B >> supply enough inactive pages. > > Oh, this was a misunderstanding. When you wrote "on second thought it > seems to be better to keep the decision at the lru level", I assumed > you were arguing for keeping the current code as-is and abandoning > your patch. > > But that leaves my questions from above unanswered. Consider the > following situation: > > A: 50M inactive 0 active > B: 0 inactive 20G active > > If the processes in A and B were not cgrouped, these pages would be on > a single LRU and we'd go after B's active pages. > > But with your patches, we'd reclaim only A's inactive pages. > I assume that not cgrouped case we would reclaim mostly from A anyway because going after B's active pages only means that we move them to inactive list where we still have A's pages for reclaim. And B has a chance to reactivate deactivated pages. In cgrouped case going after B's active pages implies immediate reclaim of them. > What's the justification for that unfairness? > If it's A creates pressure by allocating a lot of one-time used pages, than global inactive ratio check allows A to grow up to !inactive_is_low() point by pushing out B's active pages.