From patchwork Thu Jul 20 07:08:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13319871 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE42BEB64DD for ; Thu, 20 Jul 2023 07:08:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F35312800C2; Thu, 20 Jul 2023 03:08:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E98F728004C; Thu, 20 Jul 2023 03:08:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9B3E2800C2; Thu, 20 Jul 2023 03:08:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9F4B128004C for ; Thu, 20 Jul 2023 03:08:43 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 741E61A019D for ; Thu, 20 Jul 2023 07:08:43 +0000 (UTC) X-FDA: 81031112526.15.65DD1D5 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf20.hostedemail.com (Postfix) with ESMTP id B28821C001D for ; Thu, 20 Jul 2023 07:08:41 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="sV87N/Hx"; spf=pass (imf20.hostedemail.com: domain of 3eN24ZAoKCEM3txw3fmrjilttlqj.htrqnsz2-rrp0fhp.twl@flex--yosryahmed.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3eN24ZAoKCEM3txw3fmrjilttlqj.htrqnsz2-rrp0fhp.twl@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689836921; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=n6O0Lt5KhyslMSHzCGrHaCXx7KAwNPu5E55KNetzzTM=; b=vB+dP8ID25WlWjN+mwdAG4sh4YQDRUY6L2NHy8+ZKw8GfP2bESP23QOrfeTJhPYWStAz5r CnO93at83sXxPlbuq5h04jkUio9uh1lUkM16HdUY9OHDlUtUunDPIbZIFwiXb8z8h+1gdG fNDh/1W81k5tSh0ZZrMVznraWvtaptM= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="sV87N/Hx"; spf=pass (imf20.hostedemail.com: domain of 3eN24ZAoKCEM3txw3fmrjilttlqj.htrqnsz2-rrp0fhp.twl@flex--yosryahmed.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3eN24ZAoKCEM3txw3fmrjilttlqj.htrqnsz2-rrp0fhp.twl@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689836921; a=rsa-sha256; cv=none; b=hJLFA2sYFnT0Cg8Llb7BjCDAg9++FsKxWk5PfVqBgyUC2+/TGAgVFk0amneW9UVzfqvUoh ssCVx35kKbqOj3b9rLYAILoCEs8LLaNCpNOH4aSN8oL8ExT2daeWVyRFrmF1n3QPjbwZ7M DI9lzzn0q/G5k0beUUfF0PaUkqnWY1Q= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-c64ef5bde93so454564276.0 for ; Thu, 20 Jul 2023 00:08:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689836921; x=1692428921; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=n6O0Lt5KhyslMSHzCGrHaCXx7KAwNPu5E55KNetzzTM=; b=sV87N/HxxcZHHL1alJO5nBf8HzjYMdw6nkcR38Ddq2rUuAf28cTRr0rb709fkiRMW+ qEy3FdhFn01ikwkfx7y9qLguXlaMHGLFmuRtJXH+L+mW9RyHbcOJWlAUGrGs6aAu/INw 41Y2qTn8C7MjL6RvRw/y0YgIGxiJWL68zL7cWNRAYxyzZ5Cms/cFb5dUzlTJ6iCOM/ec 43odK5EQY91sLY+01W8AhKo4cVLmfc3JuvWr3teNDoKfkk1Lyj7dpYXltbO4KTOFr6c4 DheP8D0OnAwjx0EXPzUp5uV1Ui6HMxgha12C+iK56/iEaIj2+ON6C0S7v2ijUdptonk9 M9ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689836921; x=1692428921; 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=n6O0Lt5KhyslMSHzCGrHaCXx7KAwNPu5E55KNetzzTM=; b=S/j4NnRrMCLZGOWAQSiVibUDRHmfdSVZu7+4O2uZB4/fbAmLSRPRU/jK8JB/G8AIbV eBKZTPsm4O+X7oqt4KPOY4OgwFXtJZ0bc4qbcMcgViw/kB5Lqh3skXGf994x964tabu+ 5MZchWKrb3CNU7ClDwLCNLIjAurSwa8QdsYPD3POCPiDkJPnqUvkrKxajD6l9GTY0NcW 923svQCB/r+XSkuwOsiRrqEMEN/T8HbvQrse+NSTWq/JbJA9bRKZu1vE94HeC77A2MEm dkHHXZDzhdFgbosSC2tQeo6QZeOx/CIuQEVGErMH2BbnMXhruqOjvwHohRlyQTmkKC2f /gsQ== X-Gm-Message-State: ABy/qLaBeIiaGKlE46+dDKI5ndXHTYFDc7cdoHOokgWUQmwaO8i1EWYh 5VLO0OiOEWkRJh+fK6GHIyrU56D/ME3jlifW X-Google-Smtp-Source: APBJJlGMoGTZrbru1fVb9JslthbAiF2wsIl2HsR1Hk3nalG2jl6ctXDHaNYmeI9gMwOHtknP/aKqVV0pkkfu/CUf X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a5b:b8b:0:b0:c4c:f97e:421a with SMTP id l11-20020a5b0b8b000000b00c4cf97e421amr39950ybq.4.1689836920855; Thu, 20 Jul 2023 00:08:40 -0700 (PDT) Date: Thu, 20 Jul 2023 07:08:24 +0000 In-Reply-To: <20230720070825.992023-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230720070825.992023-1-yosryahmed@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230720070825.992023-8-yosryahmed@google.com> Subject: [RFC PATCH 7/8] memcg: add sysctl and config option to control memory recharging From: Yosry Ahmed To: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt Cc: Muchun Song , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Yu Zhao , Luis Chamberlain , Kees Cook , Iurii Zaikin , "T.J. Mercier" , Greg Thelen , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, Yosry Ahmed X-Rspamd-Queue-Id: B28821C001D X-Rspam-User: X-Stat-Signature: j9aosmdmnpn6a77ssw778tocyk79pk6r X-Rspamd-Server: rspam01 X-HE-Tag: 1689836921-993582 X-HE-Meta: U2FsdGVkX1+XaNBZvg5S4eckjThk8sglxMRGVCFp9Fy8CiUHvfjUwjN8JcxbiO10sTLg6a/bEJA93NIJ6e3XsrvacjoTr2tkQkHplZJ0TFSFDLtmE3yhH8CUPYSbdzDY8WwMFNoalumnDYKT8EHHDgkn236ulwcjwyrXCyoLe2TL05dlHNBHf2Pfzxa16VicN4ajaIerFuwUCuFpqX8CNzfpUloZp6R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ZuJKttmg3j09Pou+osgyMe4lv6iPSc6kN8CTF1OBEKZkNC1O/jqkO6f3d7rVu9a42g3oO8tQJE787jpYVznPZ+WtDFGHjbQwuHGevc9TyEZyJuo3FvQKNwKoJ6rZrXgZOd4FO20UqhXZVJ0wnYP55nv7ppdECnmTWk2sqwWuxCj6bCLMm4pp96mgMdRQzIsdWruB7ZjwWg+pxKJ6Xv/olPOMbUwObv98Y6YPKJci+WnV71Ng2cLMjdAl7LGMAkFAzyV2i5XqKo0X3KVV4B+8W+wC6c+1cAUm+niDYGw2ojLw7Cfo2P3GaLz3HdIpQQz59ybl/mGdo+3dwb2y7RIiQotIpMHIAyiKIdwk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add a sysctl to enable/disable memory recharging for offline memcgs. Add a config option to control whether or not it is enabled by default. Signed-off-by: Yosry Ahmed --- include/linux/memcontrol.h | 2 ++ kernel/sysctl.c | 11 +++++++++++ mm/Kconfig | 12 ++++++++++++ mm/memcontrol.c | 9 ++++++++- 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 59b653d4a76e..ae9f09ee90cb 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -60,6 +60,8 @@ struct mem_cgroup_reclaim_cookie { #ifdef CONFIG_MEMCG +extern int sysctl_recharge_offline_memcgs; + #define MEM_CGROUP_ID_SHIFT 16 #define MEM_CGROUP_ID_MAX USHRT_MAX diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 354a2d294f52..1735d1d95652 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -2249,6 +2249,17 @@ static struct ctl_table vm_table[] = { .extra2 = (void *)&mmap_rnd_compat_bits_max, }, #endif +#ifdef CONFIG_MEMCG + { + .procname = "recharge_offline_memcgs", + .data = &sysctl_recharge_offline_memcgs, + .maxlen = sizeof(sysctl_recharge_offline_memcgs), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, +#endif /* CONFIG_MEMCG */ { } }; diff --git a/mm/Kconfig b/mm/Kconfig index 09130434e30d..9462c4b598d9 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1236,6 +1236,18 @@ config LOCK_MM_AND_FIND_VMA bool depends on !STACK_GROWSUP +config MEMCG_RECHARGE_OFFLINE_ENABLED + bool "Recharge memory charged to offline memcgs" + depends on MEMCG + help + When a memory cgroup is removed by userspace, try to recharge any + memory still charged to it to avoid having it live on as an offline + memcg. Offline memcgs potentially consume memory and limit scalability + of some operations. + + This option enables the above behavior by default. It can be override + at runtime through /proc/sys/vm/recharge_offline_memcgs. + source "mm/damon/Kconfig" endmenu diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 2fe9c6f1be80..25cdb17eaaa3 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -96,6 +96,9 @@ static bool cgroup_memory_nobpf __ro_after_init; static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq); #endif +int sysctl_recharge_offline_memcgs __read_mostly = IS_ENABLED( + CONFIG_MEMCG_RECHARGE_OFFLINE_ENABLED); + static struct workqueue_struct *memcg_recharge_wq; /* Whether legacy memory+swap accounting is active */ @@ -6592,7 +6595,8 @@ static void memcg_recharge_mapped_folios(struct mem_cgroup *memcg) INIT_DELAYED_WORK(&memcg->recharge_mapped_work.dwork, memcg_do_recharge_mapped_folios); - if (memcg_recharge_wq && memcg_nr_local_mapped_pages(memcg)) { + if (sysctl_recharge_offline_memcgs && + memcg_recharge_wq && memcg_nr_local_mapped_pages(memcg)) { memcg->recharge_mapped_work.retries = 0; queue_delayed_work(memcg_recharge_wq, &memcg->recharge_mapped_work.dwork, 0); @@ -6605,6 +6609,9 @@ static bool should_do_deferred_recharge(struct folio *folio) struct mem_cgroup *memcg; bool ret; + if (!sysctl_recharge_offline_memcgs) + return false; + rcu_read_lock(); memcg = folio_memcg_rcu(folio); ret = memcg && !!(memcg->css.flags & CSS_DYING);