Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp491635ima; Thu, 31 Jan 2019 21:28:36 -0800 (PST) X-Google-Smtp-Source: AHgI3IYnkl46PXZo3vsjMJmZzGGoNNCA9j2VGQk+kIVRZkuGXVCY9KDvAt45o1nkjezmPV7EZxU5 X-Received: by 2002:a63:b4c:: with SMTP id a12mr954926pgl.131.1548998916256; Thu, 31 Jan 2019 21:28:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548998916; cv=none; d=google.com; s=arc-20160816; b=MHl3hjrLIL7opp6JUj9yTRRjUGHgUJub9yOdae8+eWfRE1W8xIZ4H+S7Rk1h342y3u VOmBEaoFLrWxq77TKiXSXRTnvPgsAmNaLmBCrZ1OK5kJHu1yPGyTi/3gG56Am8Fi0xUy 0Qzra+AngXft60SnMZgwYpkgHpncKSeilPNikqrwvIhRmKTp2/dyFKCEAx4GEx+oBUDA HiYUwPeqCXpYGYyJT1HpM95QLXZ6mTFBXPt4g/3P5UALLWR+dtskEOW1ao/RdQ3PfVfH 3IxLcrRppXDVDeI8hgGWoNIzOpwpKQj6W2huDxH9dlh8IOpEzh0xZlebWSe7z493FnaC hPsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:message-id:subject:cc:to:from:date :dkim-signature; bh=bVOuN27+dQpcYjyoBP7qcpwIAC0k6Nxrf6LI5xH+LA4=; b=f8luaULjDdrtzNBULIXdvqykuXdJcMKM2Fn4aMmGxDYXMKX9dVeEbUKUppInXBeTSl Sg+BPgtlv+g6/x1u3K+obQEy0lW221rzIMrXjpg4Y+wx+juDp0ra3oNGRa+VW5nrBZd3 31YJ4ZHT5z/YyqtlXgnZ9mn8DrLYbmBfFfbFivMJj1IomAeR6bH7ckUC89gB1OBCz0yk 3fKqfha/FGJsF+q0t8Dcx0ff4UB0w6Ziqtq4raoqo7WX3fBmJ6xxsaJFt9SvL1ee8B0w WjxX79hHBiLByunYiqRkXfy1cEye2uQr4TmHNqoetH7A3ymApAwxQPffEquWP/Xrsdop iz+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chrisdown.name header.s=google header.b=b3f8X3vk; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chrisdown.name Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g31si6646762pld.358.2019.01.31.21.28.21; Thu, 31 Jan 2019 21:28:36 -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; dkim=pass header.i=@chrisdown.name header.s=google header.b=b3f8X3vk; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chrisdown.name Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726624AbfBAFSO (ORCPT + 99 others); Fri, 1 Feb 2019 00:18:14 -0500 Received: from mail-qt1-f195.google.com ([209.85.160.195]:36463 "EHLO mail-qt1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726114AbfBAFSN (ORCPT ); Fri, 1 Feb 2019 00:18:13 -0500 Received: by mail-qt1-f195.google.com with SMTP id t13so6197440qtn.3 for ; Thu, 31 Jan 2019 21:18:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :in-reply-to:user-agent; bh=bVOuN27+dQpcYjyoBP7qcpwIAC0k6Nxrf6LI5xH+LA4=; b=b3f8X3vkGREfwb8cgrZL68JTIwv9yiDjla7EXDA9MgwWVXeSsFHmJHsi3G7GDankY5 T57lz9NGHBL4NeJ4dlqbpYJrsoEI7+Uvw2GlbxDI3rbmFQ64Yj2KIQZr7rEORjlCsHTj efE4MLs4dUIptQZ4a/SYpQh36Wod7GTl8OAbk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:in-reply-to:user-agent; bh=bVOuN27+dQpcYjyoBP7qcpwIAC0k6Nxrf6LI5xH+LA4=; b=Wwo+BJxYOw7679cVYy+eF8LLJsgSXtuYw+Fiquky6Vj197awj615VzcesDB8/9ldoG 9IcGdpysP7pk5rZUcK2bp0r65Vse6aBq5dTOdXBLlv0FVVy349FtVQ8ttq+h93lGkl51 +AC8ThcHCIIQkjnuOATqaYTPb68OQ+FYg68OSrLcQBUXGOy6euHY22jPH1BSc2epBrdA /sp0VYWLddUghmUvwvdpZCJ1cMJUq+bNJJH3/Ugkhp4xf9utU2SW/A+VxgtefXi79Gjv 1POQ7FjQxrtvss3P+rzk4yudDI0jUyUy8FJ8/3Tp6QaLpfN4HBqhuG3zVktaNbZwY5Jt 2eEw== X-Gm-Message-State: AJcUukenCvm2+B345bluDkBKrnFDmjL1HArCq4seHOoSzEciZvbruKbA UpdY6toUesI/WRLrZaZofuJ6og== X-Received: by 2002:ac8:4a10:: with SMTP id x16mr38505487qtq.164.1548998292366; Thu, 31 Jan 2019 21:18:12 -0800 (PST) Received: from localhost (rrcs-108-176-24-99.nyc.biz.rr.com. [108.176.24.99]) by smtp.gmail.com with ESMTPSA id p8sm6253906qtk.70.2019.01.31.21.18.11 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 31 Jan 2019 21:18:11 -0800 (PST) Date: Fri, 1 Feb 2019 00:18:10 -0500 From: Chris Down To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Tejun Heo , Roman Gushchin , Dennis Zhou , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH v4] mm: Make memory.emin the baseline for utilisation determination Message-ID: <20190201051810.GA18895@chrisdown.name> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190129191525.GB10430@chrisdown.name> User-Agent: Mutt/1.11.2 (2019-01-07) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Roman points out that when when we do the low reclaim pass, we scale the reclaim pressure relative to position between 0 and the maximum protection threshold. However, if the maximum protection is based on memory.elow, and memory.emin is above zero, this means we still may get binary behaviour on second-pass low reclaim. This is because we scale starting at 0, not starting at memory.emin, and since we don't scan at all below emin, we end up with cliff behaviour. This should be a fairly uncommon case since usually we don't go into the second pass, but it makes sense to scale our low reclaim pressure starting at emin. You can test this by catting two large sparse files, one in a cgroup with emin set to some moderate size compared to physical RAM, and another cgroup without any emin. In both cgroups, set an elow larger than 50% of physical RAM. The one with emin will have less page scanning, as reclaim pressure is lower. Signed-off-by: Chris Down Suggested-by: Roman Gushchin Cc: Johannes Weiner Cc: Andrew Morton Cc: Michal Hocko Cc: Tejun Heo Cc: Roman Gushchin Cc: Dennis Zhou Cc: linux-kernel@vger.kernel.org Cc: cgroups@vger.kernel.org Cc: linux-mm@kvack.org Cc: kernel-team@fb.com --- include/linux/memcontrol.h | 19 ++++++++----- mm/vmscan.c | 55 +++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 28 deletions(-) Rebase on top of and apply the same idea as what was applied to handle cgroup_memory=disable properly for the original proportional patch (20190201045711.GA18302@chrisdown.name, "mm, memcg: Handle cgroup_disable=memory when getting memcg protection"). diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 49742489aa56..0fcbea7ad0c8 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -333,12 +333,17 @@ static inline bool mem_cgroup_disabled(void) return !cgroup_subsys_enabled(memory_cgrp_subsys); } -static inline unsigned long mem_cgroup_protection(struct mem_cgroup *memcg) +static inline void mem_cgroup_protection(struct mem_cgroup *memcg, + unsigned long *min, unsigned long *low) { - if (mem_cgroup_disabled()) - return 0; + if (mem_cgroup_disabled()) { + *min = 0; + *low = 0; + return; + } - return max(READ_ONCE(memcg->memory.emin), READ_ONCE(memcg->memory.elow)); + *min = READ_ONCE(memcg->memory.emin); + *low = READ_ONCE(memcg->memory.elow); } enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root, @@ -829,9 +834,11 @@ static inline void memcg_memory_event_mm(struct mm_struct *mm, { } -static inline unsigned long mem_cgroup_protection(struct mem_cgroup *memcg) +static inline void mem_cgroup_protection(struct mem_cgroup *memcg, + unsigned long *min, unsigned long *low) { - return 0; + *min = 0; + *low = 0; } static inline enum mem_cgroup_protection mem_cgroup_protected( diff --git a/mm/vmscan.c b/mm/vmscan.c index 549251818605..f7c4ab39d5d0 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2447,12 +2447,12 @@ static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, int file = is_file_lru(lru); unsigned long lruvec_size; unsigned long scan; - unsigned long protection; + unsigned long min, low; lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); - protection = mem_cgroup_protection(memcg); + mem_cgroup_protection(memcg, &min, &low); - if (protection > 0) { + if (min || low) { /* * Scale a cgroup's reclaim pressure by proportioning * its current usage to its memory.low or memory.min @@ -2467,28 +2467,38 @@ static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, * set it too low, which is not ideal. */ unsigned long cgroup_size = mem_cgroup_size(memcg); - unsigned long baseline = 0; /* - * During the reclaim first pass, we only consider - * cgroups in excess of their protection setting, but if - * that doesn't produce free pages, we come back for a - * second pass where we reclaim from all groups. + * If there is any protection in place, we adjust scan + * pressure in proportion to how much a group's current + * usage exceeds that, in percent. * - * To maintain fairness in both cases, the first pass - * targets groups in proportion to their overage, and - * the second pass targets groups in proportion to their - * protection utilization. - * - * So on the first pass, a group whose size is 130% of - * its protection will be targeted at 30% of its size. - * On the second pass, a group whose size is at 40% of - * its protection will be - * targeted at 40% of its size. + * There is one special case: in the first reclaim pass, + * we skip over all groups that are within their low + * protection. If that fails to reclaim enough pages to + * satisfy the reclaim goal, we come back and override + * the best-effort low protection. However, we still + * ideally want to honor how well-behaved groups are in + * that case instead of simply punishing them all + * equally. As such, we reclaim them based on how much + * of their best-effort protection they are using. Usage + * below memory.min is excluded from consideration when + * calculating utilisation, as it isn't ever + * reclaimable, so it might as well not exist for our + * purposes. */ - if (!sc->memcg_low_reclaim) - baseline = lruvec_size; - scan = lruvec_size * cgroup_size / protection - baseline; + if (sc->memcg_low_reclaim && low > min) { + /* + * Reclaim according to utilisation between min + * and low + */ + scan = lruvec_size * (cgroup_size - min) / + (low - min); + } else { + /* Reclaim according to protection overage */ + scan = lruvec_size * cgroup_size / + max(min, low) - lruvec_size; + } /* * Don't allow the scan target to exceed the lruvec @@ -2504,7 +2514,8 @@ static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, * some cases in the case of large overages. * * Also, minimally target SWAP_CLUSTER_MAX pages to keep - * reclaim moving forwards. + * reclaim moving forwards, avoiding decremeting + * sc->priority further than desirable. */ scan = clamp(scan, SWAP_CLUSTER_MAX, lruvec_size); } else { -- 2.20.1