From patchwork Tue Apr 4 00:13:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 78797 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2671813vqo; Mon, 3 Apr 2023 17:28:47 -0700 (PDT) X-Google-Smtp-Source: AKy350ZUCNPEzc7EwXpEPD75keeVSW4Xz2qHHUx9yFe7vmhEd4Hy38Mo5ZOYtWZkix9Ggt0PdxAF X-Received: by 2002:a17:903:41d1:b0:1a1:7b8d:6719 with SMTP id u17-20020a17090341d100b001a17b8d6719mr773338ple.27.1680568127346; Mon, 03 Apr 2023 17:28:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680568127; cv=none; d=google.com; s=arc-20160816; b=ZY/C14LmcGcDJFNe0K5ocH+2wobqfVzpFdSKajlrysIcoRGh+tlPxOQOUicFBx2USG 4NBLXNKLz3z+Lf/3C1GK4pX2Gt4lICpA6zfa37ktfY1AYZMNB/VRZebYC8vTdeGLPi3V orBKLtMWhL9GCCoDQopAHZErGv3CKWsWbc1kAzgu0RtUoLmCalHJcHs7JDQiH7jyw1e7 sg1+nksn6oJ9HD8ZP0ZIE694OTR2cawXPTo4cSRMTsuIIM8Tls7bNSRmw/2S3j+DG6uu /ZA1Acsuql4rf1LDGOkaGlB8ElIUzMDGihf4hOfsAizNjx7Ce4FphiDb/6a7j9vvuFBj tGLg== 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=QYyKpmgAe3S55Nk5FWRs2dH0UIocesat7xjNxdFTU4k=; b=r2wFnTVeD3WqGYiL5CBBlQh4axioS/MsrT5CXn9HAn4817pW7f6IS44OD3ixeN9hJe LpZKfRT5kW7iAZiNpxikmVBRTCSMm1l1yOQsw/5BHo0W/uPLCO7UXi3bkRifAnEDZvyr Uv9qnQg1Kvun0sJFk3oTJ7xYaJZIYEfCPdGsuQabW4bco+XEkD9AraM2KULClywC4P+L 8mYPBJl8LJw77jdasxiaxOvAg6X8zoyK10DcNygZtNfl6kYlpDgBx3xedxe3kMlSKLM0 /mfBW3IazHo3mpe2gkDKtBcTx1r/h4+HnHAt9LG/j4gPII2wVSorRi/s2/VIgjmtb73G X6wA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=W5WukSX7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c3-20020a170902aa4300b001a1d6a9c6besi8727249plr.386.2023.04.03.17.28.34; Mon, 03 Apr 2023 17:28:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=W5WukSX7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231969AbjDDAOW (ORCPT + 99 others); Mon, 3 Apr 2023 20:14:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231845AbjDDAOM (ORCPT ); Mon, 3 Apr 2023 20:14:12 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FDC94497 for ; Mon, 3 Apr 2023 17:14:02 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id q30-20020a17090a17a100b0023d376ac2c5so8023274pja.5 for ; Mon, 03 Apr 2023 17:14:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680567242; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QYyKpmgAe3S55Nk5FWRs2dH0UIocesat7xjNxdFTU4k=; b=W5WukSX7xVU0/vv5ALD+tup9Pddeb9Iipi9kb8kJt/jBn+TQTiZoJyEWgKO/OQ61Jh 2tDeM9kRzvnDYGPtmMlhz5iVehjZJg/jQ63YFUQ7WbbPKq0AY2cM2cHrwogahpd+PgEG XwtqC+dGnvs0z0/3t8FCcMDB2O7fMVf3H3E2qFVcjwb9hPLZO3zgEa2XrTMXRi9MFCHf WEtCE6c9sD8f7AqxvmgV8c+yt8qmrF2I9lAqEvjVuhsI1HciUBrepOGZet31fNCDDW8P oX+wUwY7aO9vBuigwgimgUP/rKThFLYxYAVKIiGShyp9evKm8iqS6ChI4jTiXqDaT7MQ ct3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680567242; 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=QYyKpmgAe3S55Nk5FWRs2dH0UIocesat7xjNxdFTU4k=; b=11CMZadpfZHeeuZe8uO1MMsaXQCYwQFGOBZFUb9/IrRlgNmaORSuMZsrUXC9fg5ckI I7JinSrAjAmrFcXtY+ITJ6USSzwTylRCvvfbSZAestPZRhBcAZiSLQNLZHvLESfP6wja MGsDXvg3sdyscHeS2OveMqyQ/6r/tAJqcj9PShcmMGcql7lRIjSZJxV4InMBYaSnnzuf Z/MiIlMg0ULoCB8Pc8ugPtDMiaCYmHlqHO4zgJb6JDIXzrwXZBCKhL87jd8Ovama2ab2 aD4wLgz9OZ4eFzL5TmCj6kFR3n3Re13vpN/PWh6VEHs+e5uOOQvdftnlA2X3aBjM4o1+ YaQQ== X-Gm-Message-State: AAQBX9ce0ni5e2JKrqb04s3HVOSDVBxjPTZsFknWKdoR49GfX0hY9Rzo 9iTmPyEeCU5a/KLDFfmyhcIIZFiviEi75OAt X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a05:6a00:2e14:b0:627:e6d5:ba2d with SMTP id fc20-20020a056a002e1400b00627e6d5ba2dmr223605pfb.6.1680567241767; Mon, 03 Apr 2023 17:14:01 -0700 (PDT) Date: Tue, 4 Apr 2023 00:13:53 +0000 In-Reply-To: <20230404001353.468224-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230404001353.468224-1-yosryahmed@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230404001353.468224-4-yosryahmed@google.com> Subject: [PATCH v4 3/3] mm: vmscan: ignore non-LRU-based reclaim in memcg reclaim From: Yosry Ahmed To: Andrew Morton , Alexander Viro , "Darrick J. Wong" , Christoph Lameter , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, "Matthew Wilcox (Oracle)" , Miaohe Lin , David Hildenbrand , Johannes Weiner , Peter Xu , NeilBrown , Shakeel Butt , Michal Hocko , Yu Zhao , Dave Chinner Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-mm@kvack.org, Yosry Ahmed X-Spam-Status: No, score=-7.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762203404807036307?= X-GMAIL-MSGID: =?utf-8?q?1762203404807036307?= We keep track of different types of reclaimed pages through reclaim_state->reclaimed, and we add them to the reported number of reclaimed pages. For non-memcg reclaim, this makes sense. For memcg reclaim, we have no clue if those pages are charged to the memcg under reclaim. Slab pages are shared by different memcgs, so a freed slab page may have only been partially charged to the memcg under reclaim. The same goes for clean file pages from pruned inodes (on highmem systems) or xfs buffer pages, there is no simple way to currently link them to the memcg under reclaim. Stop reporting those freed pages as reclaimed pages during memcg reclaim. This should make the return value of writing to memory.reclaim, and may help reduce unnecessary reclaim retries during memcg charging. Writing to memory.reclaim on the root memcg is considered as cgroup_reclaim(), but for this case we want to include any freed pages, so use the global_reclaim() check instead. Generally, this should make the return value of try_to_free_mem_cgroup_pages() more accurate. In some limited cases (e.g. freed a slab page that was mostly charged to the memcg under reclaim), the return value of try_to_free_mem_cgroup_pages() can be underestimated, but this should be fine. The freed pages will be uncharged anyway, and we can charge the memcg the next time around as we usually do memcg reclaim in a retry loop. Signed-off-by: Yosry Ahmed --- mm/vmscan.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 8f0e7c4e91ae3..049e39202e6ce 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -518,7 +518,35 @@ static void set_task_reclaim_state(struct task_struct *task, static void flush_reclaim_state(struct scan_control *sc, struct reclaim_state *rs) { - if (rs) { + /* + * Currently, reclaim_state->reclaimed includes three types of pages + * freed outside of vmscan: + * (1) Slab pages. + * (2) Clean file pages from pruned inodes. + * (3) XFS freed buffer pages. + * + * For all of these cases, we have no way of finding out whether these + * pages were related to the memcg under reclaim. For example, a freed + * slab page could have had only a single object charged to the memcg + * under reclaim. Also, populated inodes are not on shrinker LRUs + * anymore except on highmem systems. + * + * Instead of over-reporting the reclaimed pages in a memcg reclaim, + * only count such pages in global reclaim. This prevents unnecessary + * retries during memcg charging and false positive from proactive + * reclaim (memory.reclaim). + * + * For uncommon cases were the freed pages were actually significantly + * charged to the memcg under reclaim, and we end up under-reporting, it + * should be fine. The freed pages will be uncharged anyway, even if + * they are not reported properly, and we will be able to make forward + * progress in charging (which is usually in a retry loop). + * + * We can go one step further, and report the uncharged objcg pages in + * memcg reclaim, to make reporting more accurate and reduce + * under-reporting, but it's probably not worth the complexity for now. + */ + if (rs && global_reclaim(sc)) { sc->nr_reclaimed += rs->reclaimed; rs->reclaimed = 0; }