From patchwork Thu Jul 20 07:27:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Efly Young X-Patchwork-Id: 123082 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2962893vqt; Thu, 20 Jul 2023 01:14:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlEwPlNVwDy114K7s5GNRkelg5VpJDpV9Rav/omzHxK08nCmL2MALs17bWO1rhblhOFKgi7i X-Received: by 2002:a05:6512:252b:b0:4fb:a990:bb28 with SMTP id be43-20020a056512252b00b004fba990bb28mr1208574lfb.18.1689840857927; Thu, 20 Jul 2023 01:14:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689840857; cv=none; d=google.com; s=arc-20160816; b=e1g4MS0vc9zVxGQB3NeP6xUQSwpelG5gnSvHEhdYZ8DmAWljWs+ckYQTQl51Jzu+C7 6b73w1zviqA/cgTxJr1QI3UrsKymRLkwHNHDJilT3cQEJKPV7kSLDeRJzoTbT7KlTqOW fx0ZUG6M86cJ8ZjAR2kKAlbRtFlAXNUwca7AyeksD5uErC1okdBpOa6uw0su3pBXjA5P kxRovfpHQozKkEfI8u9dJr6Y2xaD0e5S5imL9+0xYFY17LeMH5NJS6W6VvmoSxuzWKM9 BL0wdMsY7Mzx80d0mMawCO3cU6DmdpgamJurC3eskUU7do6wr9CjFULvwf1lTmbF+pVu my7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=2hvt2nAb9FQGc8JWV2zRVOy7HcAR3TW4z5ZsrMla/mw=; fh=6PrjyxLRmdyn5MqncsnCK9iGkYOASTzc/cy7qHLX5C8=; b=J1oGxMw7BMq62Q2wA9zso7uHlHk/Nlz217UK/9woFg1KOREQIini3k2zkuTU+SRTH+ u6uZzT90Mxirdy+HrZxuPkqYQdlcDOEFWRRFFxI/3F2zm/tpxPjJGHzzFta7z0QgfWp/ 1fBfe4qMdZd8IWqFTeyGQMV/KR+OSmo0WAmK1WUBUQ9SDl1uGwEIx++KNjl0A6mj4Njq 4hO8LloZhZerLtM0RfG9AK2IgFTXoOqi9JE78GJyByk6RYvdlrU9vkeVllRkFAbYSLjy uOkTlousU1tK09/jJZLnR88202lUtK3kkFWm4lpxioWg+oeLAQ0/v/4bB8HvA3+GRUq7 bMVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kuaishou.com header.s=dkim header.b=Ov9ON0jO; 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=NONE sp=NONE dis=NONE) header.from=kuaishou.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h1-20020a50ed81000000b005149e1735bcsi510417edr.33.2023.07.20.01.13.54; Thu, 20 Jul 2023 01:14:17 -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=@kuaishou.com header.s=dkim header.b=Ov9ON0jO; 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=NONE sp=NONE dis=NONE) header.from=kuaishou.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231824AbjGTHwn (ORCPT + 99 others); Thu, 20 Jul 2023 03:52:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231814AbjGTHwk (ORCPT ); Thu, 20 Jul 2023 03:52:40 -0400 X-Greylist: delayed 1367 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Thu, 20 Jul 2023 00:52:37 PDT Received: from bjm7-spam02.kuaishou.com (smtpcn03.kuaishou.com [103.107.217.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93FA52135; Thu, 20 Jul 2023 00:52:37 -0700 (PDT) Received: from bjm7-spam02.kuaishou.com (localhost [127.0.0.2] (may be forged)) by bjm7-spam02.kuaishou.com with ESMTP id 36K7TohW080274; Thu, 20 Jul 2023 15:29:50 +0800 (GMT-8) (envelope-from yangyifei03@kuaishou.com) Received: from bjm7-pm-mail12.kuaishou.com ([172.28.1.94]) by bjm7-spam02.kuaishou.com with ESMTPS id 36K7RmOj078771 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 20 Jul 2023 15:27:54 +0800 (GMT-8) (envelope-from yangyifei03@kuaishou.com) DKIM-Signature: v=1; a=rsa-sha256; d=kuaishou.com; s=dkim; c=relaxed/relaxed; t=1689838068; h=from:subject:to:date:message-id; bh=2hvt2nAb9FQGc8JWV2zRVOy7HcAR3TW4z5ZsrMla/mw=; b=Ov9ON0jOMvPY7tYxtSF27RXIYX5/ESwleAfyzbMcRkfahhvPD8+vCRgUolm4nuFAX7xrxXLLqLP VC0ugqxGe1jauqIjydi7S9YH2M2vjCjTteErwqG546qEglqgQx2dxnNycFZVOCvZX9fbPRgt69Um7 2ew2dB1E6Kom/DkdCz0= Received: from public-bjmt-d51.idcyz.hb1.kwaidc.com (172.28.1.32) by bjm7-pm-mail12.kuaishou.com (172.28.1.94) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.20; Thu, 20 Jul 2023 15:27:48 +0800 From: Efly Young To: CC: , , , Subject: [PATCH] mm:vmscan: fix inaccurate reclaim during proactive reclaim Date: Thu, 20 Jul 2023 15:27:08 +0800 Message-ID: <20230720072708.55067-1-yangyifei03@kuaishou.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Originating-IP: [172.28.1.32] X-ClientProxiedBy: bjxm-pm-mail02.kuaishou.com (172.28.128.2) To bjm7-pm-mail12.kuaishou.com (172.28.1.94) X-DNSRBL: X-SPAM-SOURCE-CHECK: pass X-MAIL: bjm7-spam02.kuaishou.com 36K7TohW080274 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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: INBOX X-GMAIL-THRID: 1771926567293001697 X-GMAIL-MSGID: 1771926567293001697 Before commit f53af4285d77 ("mm: vmscan: fix extreme overreclaim and swap floods"), proactive reclaim will extreme overreclaim sometimes. But proactive reclaim still inaccurate and some extent overreclaim. Problematic case is easy to construct. Allocate lots of anonymous memory (e.g., 20G) in a memcg, then swapping by writing memory.recalim and there is a certain probability of overreclaim. For example, request 1G by writing memory.reclaim will eventually reclaim 1.7G or other values more than 1G. The reason is that reclaimer may have already reclaimed part of requested memory in one loop, but before adjust sc->nr_to_reclaim in outer loop, call shrink_lruvec() again will still follow the current sc->nr_to_reclaim to work. It will eventually lead to overreclaim. In theory, the amount of reclaimed would be in [request, 2 * request). Reclaimer usually tends to reclaim more than request. But either direct or kswapd reclaim have much smaller nr_to_reclaim targets, so it is less noticeable and not have much impact. Proactive reclaim can usually come in with a larger value, so the error is difficult to ignore. Considering proactive reclaim is usually low frequency, handle the batching into smaller chunks is a better approach. Signed-off-by: Efly Young Signed-off-by: Johannes Weiner Acked-by: Johannes Weiner --- mm/memcontrol.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 4b27e24..d36cf88 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6741,8 +6741,8 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, lru_add_drain_all(); reclaimed = try_to_free_mem_cgroup_pages(memcg, - nr_to_reclaim - nr_reclaimed, - GFP_KERNEL, reclaim_options); + min(nr_to_reclaim - nr_reclaimed, SWAP_CLUSTER_MAX), + GFP_KERNEL, reclaim_options); if (!reclaimed && !nr_retries--) return -EAGAIN;