Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2146772rdb; Thu, 7 Dec 2023 22:14:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IG5bx3Y6Iea0NsI06iBNQyu/TN/clVjjxlf+kVdEK8BVub1JfHWjCb99FdkK21bOXa1LPiA X-Received: by 2002:a17:902:e741:b0:1d0:c888:d11d with SMTP id p1-20020a170902e74100b001d0c888d11dmr581190plf.29.1702016084087; Thu, 07 Dec 2023 22:14:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702016084; cv=none; d=google.com; s=arc-20160816; b=R1Z1svsvLW+B0eLHd1xy5ruDxOQJQFuvIIAHzcRXSz4KBtnCPDx7ll4wc2jTQczFcN tUP4T4ak5JOBah0y2ELw4zEip8KVD9r5LIXP6IyLA9+7cyXa6aJRXpkHLWHDQoyzJgDL mXI/TDpLdA6he6fsaOmLmwJswCJ+tzSvcNMzaecQIzP88LxDWVONxZJ0mR77qlx1E5hh wSoKEOj40fUkF0bRWpT6/D8DJ5Sut20fAfuj2SKZx66KAxyB7JexXK5mL8+QzLK4161q ql67SSpSLPcZIRNU8k7n0ZXhqn1Ue4vQtmN+OcYwHhdEFQNohkTDQSyJAiSS60FBfw2B M2uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=XyO/ljh9FKCeCHVPdhFy9bPgiCVl0Vc+13vD9r1JAPY=; fh=y0ulJ+i/IiXdhkZjff7CPUVioFi+JhpR4i6gNnaoQh0=; b=c7zNyBdvG3VWqqhqVUpmCt0GZObMZ/xduBq1nV8yLb9esqtgQJhursl/jzLxO73kOY qworhQLTMLnZXjwPvawRpw3HmktoyVYXU6HWVDoCebS/HduMbBdFTqtIDvzCOLxLkNPM IUJ1+ilFs5b85+bkUWZI86kkk5xA1gzVInVGKx28/iTrzuskafLEt9bdjln9bPh/JMVc XS4JBkHvHwJe4890Uun9fkJEhUuHZvxGYt4sJyojJzRAVL6q+Sk2gzSZQaTCPW6j31tb 8o+1S9Xvz3yMy9AzDqEd3Xcwryxm9In4fouYGhsKt/8WJh/bCiFLA8PpaMO3BRyXojdf aC1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GUYrbsor; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id p10-20020a170902eaca00b001cc2f9d6a66si1009214pld.514.2023.12.07.22.14.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 22:14:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GUYrbsor; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 655BF833D576; Thu, 7 Dec 2023 22:14:41 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573209AbjLHGOS (ORCPT + 99 others); Fri, 8 Dec 2023 01:14:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233113AbjLHGOO (ORCPT ); Fri, 8 Dec 2023 01:14:14 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA9E11722 for ; Thu, 7 Dec 2023 22:14:20 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d7e7e10231so11270377b3.1 for ; Thu, 07 Dec 2023 22:14:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702016060; x=1702620860; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=XyO/ljh9FKCeCHVPdhFy9bPgiCVl0Vc+13vD9r1JAPY=; b=GUYrbsor2KKaC7Oq15b/4pulJKhW+gjsCS0minnbPPwyvsHQFaZzo35B6Wrlaqc3bG ItvYxkU+76n3Gh/uZHpFEDogn6aZo9mMc3qbHzjaFYGQbhtia40wLFS9uGoSVCTs7dwQ PYJhdgNM5xAT1dQlLe07Yn03/E+LTNyxZn6cTs9EWKDZqInOttTFuT+H/C6WD2rx7aBZ TwTYMvJUIcBsfGP8pHpDUr+9nsqnx6o577bt/yZ9mi+L4sGML9Kk/JP44HPVBhXaAzSO dU1FcKVBTT4wm2ZSrl2Bi34BH1HLlEamXWcWlNHdEjmn0DXF5dkorJAp4HYCDis/F9uY stfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702016060; x=1702620860; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XyO/ljh9FKCeCHVPdhFy9bPgiCVl0Vc+13vD9r1JAPY=; b=HWiDkQRTlFtF0iZPV9UP4nA9FuyqFVBMoyg8orfOygEjpjPIN4ZfI7DfeAJihBtHJg IIYh84QZLqvVasVM96JelCw7YC1enF685uXT6xxc1tqBAMykoyLk26qzGNz0uIIXFqpf orgA4q9ioci3bB0v7SfcfSMVAA09Ub9UiMDefhxHR7BCNfM0GjWX4/FAFSh1S4nxPWNy pfuznQR0Wv9cgRj0qemCnBwgWBTpUf9mgBaqlB8W1GmAuKqUctiGEOf7CZ7PWJgtFrB4 RVgqrayKQXNaBC9ST9nX5jpvVMpqdoz2Fegn2SSV+xCV4oSg3IBSB0bwswKMqGRfX4Lp DsKQ== X-Gm-Message-State: AOJu0YwXu1oXACt/gh/9OW+W3sw/Jw5UnQR9GqjA7iQM/QAegP5dWNi9 gkz5HaGDcCGu/N8AoGxpj6FmKRefSok= X-Received: from yuzhao2.bld.corp.google.com ([100.64.188.49]) (user=yuzhao job=sendgmr) by 2002:a05:690c:d05:b0:5d3:e8b8:e1fd with SMTP id cn5-20020a05690c0d0500b005d3e8b8e1fdmr7250ywb.3.1702016060001; Thu, 07 Dec 2023 22:14:20 -0800 (PST) Date: Thu, 7 Dec 2023 23:14:07 -0700 In-Reply-To: <20231208061407.2125867-1-yuzhao@google.com> Mime-Version: 1.0 References: <20231208061407.2125867-1-yuzhao@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231208061407.2125867-4-yuzhao@google.com> Subject: [PATCH mm-unstable v1 4/4] mm/mglru: reclaim offlined memcgs harder From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , "T . J . Mercier" , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 07 Dec 2023 22:14:41 -0800 (PST) In the effort to reduce zombie memcgs [1], it was discovered that the memcg LRU doesn't apply enough pressure on offlined memcgs. Specifically, instead of rotating them to the tail of the current generation (MEMCG_LRU_TAIL) for a second attempt, it moves them to the next generation (MEMCG_LRU_YOUNG) after the first attempt. Not applying enough pressure on offlined memcgs can cause them to build up, and this can be particularly harmful to memory-constrained systems. On Pixel 8 Pro, launching apps for 50 cycles: Before After Change Zombie memcgs 45 35 -22% [1] https://lore.kernel.org/CABdmKX2M6koq4Q0Cmp_-=wbP0Qa190HdEGGaHfxNS05gAkUtPA@mail.gmail.com/ Fixes: e4dde56cd208 ("mm: multi-gen LRU: per-node lru_gen_folio lists") Signed-off-by: Yu Zhao Reported-by: T.J. Mercier Tested-by: T.J. Mercier Cc: stable@vger.kernel.org --- include/linux/mmzone.h | 8 ++++---- mm/vmscan.c | 24 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index e3093ef9530f..2efd3be484fd 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -524,10 +524,10 @@ void lru_gen_look_around(struct page_vma_mapped_walk *pvmw); * 1. Exceeding the soft limit, which triggers MEMCG_LRU_HEAD; * 2. The first attempt to reclaim a memcg below low, which triggers * MEMCG_LRU_TAIL; - * 3. The first attempt to reclaim a memcg below reclaimable size threshold, - * which triggers MEMCG_LRU_TAIL; - * 4. The second attempt to reclaim a memcg below reclaimable size threshold, - * which triggers MEMCG_LRU_YOUNG; + * 3. The first attempt to reclaim a memcg offlined or below reclaimable size + * threshold, which triggers MEMCG_LRU_TAIL; + * 4. The second attempt to reclaim a memcg offlined or below reclaimable size + * threshold, which triggers MEMCG_LRU_YOUNG; * 5. Attempting to reclaim a memcg below min, which triggers MEMCG_LRU_YOUNG; * 6. Finishing the aging on the eviction path, which triggers MEMCG_LRU_YOUNG; * 7. Offlining a memcg, which triggers MEMCG_LRU_OLD. diff --git a/mm/vmscan.c b/mm/vmscan.c index cac38e9cac86..dad4b80b04cd 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4626,7 +4626,12 @@ static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, } /* try to scrape all its memory if this memcg was deleted */ - *nr_to_scan = mem_cgroup_online(memcg) ? (total >> sc->priority) : total; + if (!mem_cgroup_online(memcg)) { + *nr_to_scan = total; + return false; + } + + *nr_to_scan = total >> sc->priority; /* * The aging tries to be lazy to reduce the overhead, while the eviction @@ -4747,14 +4752,9 @@ static int shrink_one(struct lruvec *lruvec, struct scan_control *sc) bool success; unsigned long scanned = sc->nr_scanned; unsigned long reclaimed = sc->nr_reclaimed; - int seg = lru_gen_memcg_seg(lruvec); struct mem_cgroup *memcg = lruvec_memcg(lruvec); struct pglist_data *pgdat = lruvec_pgdat(lruvec); - /* see the comment on MEMCG_NR_GENS */ - if (!lruvec_is_sizable(lruvec, sc)) - return seg != MEMCG_LRU_TAIL ? MEMCG_LRU_TAIL : MEMCG_LRU_YOUNG; - mem_cgroup_calculate_protection(NULL, memcg); if (mem_cgroup_below_min(NULL, memcg)) @@ -4762,7 +4762,7 @@ static int shrink_one(struct lruvec *lruvec, struct scan_control *sc) if (mem_cgroup_below_low(NULL, memcg)) { /* see the comment on MEMCG_NR_GENS */ - if (seg != MEMCG_LRU_TAIL) + if (lru_gen_memcg_seg(lruvec) != MEMCG_LRU_TAIL) return MEMCG_LRU_TAIL; memcg_memory_event(memcg, MEMCG_LOW); @@ -4778,7 +4778,15 @@ static int shrink_one(struct lruvec *lruvec, struct scan_control *sc) flush_reclaim_state(sc); - return success ? MEMCG_LRU_YOUNG : 0; + if (success && mem_cgroup_online(memcg)) + return MEMCG_LRU_YOUNG; + + if (!success && lruvec_is_sizable(lruvec, sc)) + return 0; + + /* one retry if offlined or too small */ + return lru_gen_memcg_seg(lruvec) != MEMCG_LRU_TAIL ? + MEMCG_LRU_TAIL : MEMCG_LRU_YOUNG; } #ifdef CONFIG_MEMCG -- 2.43.0.472.g3155946c3a-goog