Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp889696ybh; Mon, 13 Jul 2020 04:06:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJnrW19E4eAWJXiqgGHGmldYR0tTpCWB2n24CmjmEMe5VKluIXe+W242PLYzO8XL9vk1u0 X-Received: by 2002:aa7:c4d8:: with SMTP id p24mr85934291edr.323.1594638392180; Mon, 13 Jul 2020 04:06:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594638392; cv=none; d=google.com; s=arc-20160816; b=HLpnf9sufwJ70JJEV2oQOs6FJ62wQ7L4uezTjTpHjm08XcPS8xqag+vUylTnfXGP+p m1hphrOa6NdbPHswAu1apwj9T+WA3AR0B9GkSnhbE9N+Aa0FBImX2pBqHkiCti52XjgK YiPRnsEqFNsfKkMWCcx87POppcRj/gAV44WRPKJaCaesDxbfyhX+FmigHXCo8Mt9J7g3 UXJc12LnoMVcqTXzwKIlFeEFVt7a5N5KVbwgwRLpiVmMV5FnDGuUWMSKEOSDfNrLq/Ng mehquC4IfI7oK3utRI6wfawDW9VY8pQxvKchgW+m/PjtMLk0snqFc19Sgh4U5IBzIMSU N99g== 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:references:message-id:subject:cc :to:date:from:dkim-signature; bh=aFXMEHhkBKemwA/QoFtxiRqyIHqOIfwBr0LYDpNQG/w=; b=BxKzRDKNZ7iAt0pNeo8AZwmdPr8xq4v9DI7CIaSExsYs8b22kLXHGAxKhE1l5SO7ro 2DzKowNK9semokO0JXu0yNCgdnkUMKG9+6PNZB1rIgiqPJCl3OkxGUNDhJAlSXW6l3D5 yymGs6NDLDTTc6Rlj4IKoPZINt8/Aeeo+sLXhepa8kF8pwBZYoSV9PWh9TQSF+KSX1YE 8d5OAbKHjJbLXUVVpJd+1QXaanCMdsmp2FAGsnZfNG9G6BUR+4Bqippd7RhFgTG9dqxK a1bdHIbFC0nPvOBnAUeQwmZMSK41mJRxIrkdWFabJ7RIJfF7pwhG66fsAD3zRq+a+WGj BAjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chrisdown.name header.s=google header.b=AT52M10G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id z5si8174760eje.126.2020.07.13.04.06.08; Mon, 13 Jul 2020 04:06:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chrisdown.name header.s=google header.b=AT52M10G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1729649AbgGMLF6 (ORCPT + 99 others); Mon, 13 Jul 2020 07:05:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728714AbgGMLF5 (ORCPT ); Mon, 13 Jul 2020 07:05:57 -0400 Received: from mail-ed1-x541.google.com (mail-ed1-x541.google.com [IPv6:2a00:1450:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C3E0C061755 for ; Mon, 13 Jul 2020 04:05:57 -0700 (PDT) Received: by mail-ed1-x541.google.com with SMTP id e22so13119268edq.8 for ; Mon, 13 Jul 2020 04:05:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; h=from:date:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=aFXMEHhkBKemwA/QoFtxiRqyIHqOIfwBr0LYDpNQG/w=; b=AT52M10Glz6TiDoZ4uGS3qPTmg2vqCLijtoFBQ8ribBod0g/b47E9eyS4hn+Po7CIZ TfzqlI8FPBvDocG12P2yQK9i58fLZPBMa/ODoiYM4PgyrcNGC48huN8RO/sJncf/9QA/ Hz1D17cq1FNCR9cfEpnzBpuSAXsA5oUre28c8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=aFXMEHhkBKemwA/QoFtxiRqyIHqOIfwBr0LYDpNQG/w=; b=OiWlTVKwiK3pZ6b5ShlO4hUrzwhdjoaxSi7y8S8lr8PzFhPMmwKCoQe08VbdkoFLuV hrNUUwGPUebou7+71tkfADm22srbo/u4qS7W8sh9bLF0K6lw8cAelId4Laz1A9/NrcPQ RXbOjnLUS0f5rYbwDLlKalOI0GNlm0Orn1tO9y0gH64+XWG/C0RT6CgOR/jktasqOeGz EnF3/dF21N/0KLeWlrxcgK2GM/SeQ4S4wgF7cTAouGqepEuaGDPPN/DyfyWkoGWGsgFl gWHSgHiKL30nWWCYIcfXSkJzjxDEl45+AeKm5+OQGwkUyieVHWUemarNk011fX1Ma7Al RI6w== X-Gm-Message-State: AOAM530gCnst4Q4csQAfxD1KynmA49/OYYh55N6oOxBI3b+zE/eJ+wZU tvyHP+yJyQMOoDdlys25hE0aQg== X-Received: by 2002:a50:ab52:: with SMTP id t18mr92428592edc.195.1594638356061; Mon, 13 Jul 2020 04:05:56 -0700 (PDT) Received: from localhost ([2620:10d:c093:400::5:ef88]) by smtp.gmail.com with ESMTPSA id bq8sm9165988ejb.103.2020.07.13.04.05.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2020 04:05:55 -0700 (PDT) From: Yafang Shao X-Google-Original-From: Yafang Shao Date: Mon, 13 Jul 2020 12:05:54 +0100 To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Yafang Shao , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/2] mm, memcg: Avoid stale protection values when cgroup is above protection Message-ID: <044fb8ecffd001c7905d27c0c2ad998069fdc396.1594638158.git.chris@chrisdown.name> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.14.5 (2020-06-23) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yafang Shao A cgroup can have both memory protection and a memory limit to isolate it from its siblings in both directions - for example, to prevent it from being shrunk below 2G under high pressure from outside, but also from growing beyond 4G under low pressure. Commit 9783aa9917f8 ("mm, memcg: proportional memory.{low,min} reclaim") implemented proportional scan pressure so that multiple siblings in excess of their protection settings don't get reclaimed equally but instead in accordance to their unprotected portion. During limit reclaim, this proportionality shouldn't apply of course: there is no competition, all pressure is from within the cgroup and should be applied as such. Reclaim should operate at full efficiency. However, mem_cgroup_protected() never expected anybody to look at the effective protection values when it indicated that the cgroup is above its protection. As a result, a query during limit reclaim may return stale protection values that were calculated by a previous reclaim cycle in which the cgroup did have siblings. When this happens, reclaim is unnecessarily hesitant and potentially slow to meet the desired limit. In theory this could lead to premature OOM kills, although it's not obvious this has occurred in practice. Workaround the problem by special casing reclaim roots in mem_cgroup_protection. These memcgs are never participating in the reclaim protection because the reclaim is internal. We have to ignore effective protection values for reclaim roots because mem_cgroup_protected might be called from racing reclaim contexts with different roots. Calculation is relying on root -> leaf tree traversal therefore top-down reclaim protection invariants should hold. The only exception is the reclaim root which should have effective protection set to 0 but that would be problematic for the following setup: Let's have global and A's reclaim in parallel: | A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G) |\ | C (low = 1G, usage = 2.5G) B (low = 1G, usage = 0.5G) for A reclaim we have B.elow = B.low C.elow = C.low For the global reclaim A.elow = A.low B.elow = min(B.usage, B.low) because children_low_usage <= A.elow C.elow = min(C.usage, C.low) With the effective values resetting we have A reclaim A.elow = 0 B.elow = B.low C.elow = C.low and global reclaim could see the above and then B.elow = C.elow = 0 because children_low_usage > A.elow Which means that protected memcgs would get reclaimed. In future we would like to make mem_cgroup_protected more robust against racing reclaim contexts but that is likely more complex solution than this simple workaround. [hannes@cmpxchg.org - large part of the changelog] [mhocko@suse.com - workaround explanation] [chris@chrisdown.name - retitle] Fixes: 9783aa9917f8 ("mm, memcg: proportional memory.{low,min} reclaim") Signed-off-by: Yafang Shao Acked-by: Michal Hocko Acked-by: Johannes Weiner Acked-by: Chris Down Acked-by: Roman Gushchin Signed-off-by: Chris Down --- include/linux/memcontrol.h | 42 ++++++++++++++++++++++++++++++++++++-- mm/memcontrol.c | 8 ++++++++ mm/vmscan.c | 3 ++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index b8f52a3fed90..33d834a187e5 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -363,12 +363,49 @@ 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 unsigned long mem_cgroup_protection(struct mem_cgroup *root, + struct mem_cgroup *memcg, bool in_low_reclaim) { if (mem_cgroup_disabled()) return 0; + /* + * There is no reclaim protection applied to a targeted reclaim. + * We are special casing this specific case here because + * mem_cgroup_protected calculation is not robust enough to keep + * the protection invariant for calculated effective values for + * parallel reclaimers with different reclaim target. This is + * especially a problem for tail memcgs (as they have pages on LRU) + * which would want to have effective values 0 for targeted reclaim + * but a different value for external reclaim. + * + * Example + * Let's have global and A's reclaim in parallel: + * | + * A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G) + * |\ + * | C (low = 1G, usage = 2.5G) + * B (low = 1G, usage = 0.5G) + * + * For the global reclaim + * A.elow = A.low + * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow + * C.elow = min(C.usage, C.low) + * + * With the effective values resetting we have A reclaim + * A.elow = 0 + * B.elow = B.low + * C.elow = C.low + * + * If the global reclaim races with A's reclaim then + * B.elow = C.elow = 0 because children_low_usage > A.elow) + * is possible and reclaiming B would be violating the protection. + * + */ + if (root == memcg) + return 0; + if (in_low_reclaim) return READ_ONCE(memcg->memory.emin); @@ -899,7 +936,8 @@ static inline void memcg_memory_event_mm(struct mm_struct *mm, { } -static inline unsigned long mem_cgroup_protection(struct mem_cgroup *memcg, +static inline unsigned long mem_cgroup_protection(struct mem_cgroup *root, + struct mem_cgroup *memcg, bool in_low_reclaim) { return 0; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 0145a77aa074..21b620e36aa0 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6566,6 +6566,14 @@ enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root, if (!root) root = root_mem_cgroup; + + /* + * Effective values of the reclaim targets are ignored so they + * can be stale. Have a look at mem_cgroup_protection for more + * details. + * TODO: calculation should be more robust so that we do not need + * that special casing. + */ if (memcg == root) return MEMCG_PROT_NONE; diff --git a/mm/vmscan.c b/mm/vmscan.c index 5215840ee217..89921a12acae 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2326,7 +2326,8 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, unsigned long protection; lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); - protection = mem_cgroup_protection(memcg, + protection = mem_cgroup_protection(sc->target_mem_cgroup, + memcg, sc->memcg_low_reclaim); if (protection) { -- 2.27.0