From patchwork Sun Jun 23 20:45:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13708752 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 C7F63C2BA18 for ; Sun, 23 Jun 2024 20:46:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A46F36B04B9; Sun, 23 Jun 2024 16:45:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D37A6B04BA; Sun, 23 Jun 2024 16:45:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 870D66B04BB; Sun, 23 Jun 2024 16:45:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 660516B04B9 for ; Sun, 23 Jun 2024 16:45:59 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D06D01C140B for ; Sun, 23 Jun 2024 20:45:58 +0000 (UTC) X-FDA: 82263335196.01.7B13CC9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 2CEB7160005 for ; Sun, 23 Jun 2024 20:45:57 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Sbquofpz; spf=pass (imf08.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719175544; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=AmwdkZVL5pji4wXYmdoU+z7q/J2B7KB9OGbcUZv8AJU=; b=1sk45+MUj15k0DSL+F0koGbr0YAJkW64DQ8hKMC+amFl3ejv6Ma6nvsP+KoZZhYuEbByQz xaTXhhl0MxQCbxaCaMzH7Zs/DX17Hz/sNSRKxzkruw0PEFi7sqCPBZrHjC1CGOQzLyiGYC CR3Dnin9P4W4zlkIwtxWlbB2pELFM9o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719175544; a=rsa-sha256; cv=none; b=eeauzvoG8BFDdY+uEQPfMm5pD98zQuE0q/tbqKEPA35wD/b2iDFJsqhE9BVNak6tVBxpZL OrkyRhuVVo7mI80cpA+N5BtG4jGBYC56WzC6pjda47G4i+W8DdZ8AiRZpes9upMw2PA2qs mmn7k4Pu2YjCFrHHkykhrW/rYtdxtJs= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Sbquofpz; spf=pass (imf08.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719175556; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=AmwdkZVL5pji4wXYmdoU+z7q/J2B7KB9OGbcUZv8AJU=; b=SbquofpzdBOiqxVI2AFeKzGm3UIFmxJpOgdv22gR741YgNwmURoD6y/Q8G5Q7T6LuLcqH7 QN3Rdl9ZqQsbhzGrZrflMvZ54vDlVhpVJxKRwFDSdtGnDw3Gj6RLhtjc/FNWRoDSJxbpwb j3wOo3/p5P/9VH9gm+BJh94H57GuBgY= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-381-wl9rvSoCNPCrgih4Gu1Jrg-1; Sun, 23 Jun 2024 16:45:52 -0400 X-MC-Unique: wl9rvSoCNPCrgih4Gu1Jrg-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 26E48195608B; Sun, 23 Jun 2024 20:45:50 +0000 (UTC) Received: from llong.com (unknown [10.22.16.52]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AACD619560AE; Sun, 23 Jun 2024 20:45:46 +0000 (UTC) From: Waiman Long To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Jonathan Corbet Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-doc@kvack.org, Alex Kalenyuk , Peter Hunt , Waiman Long Subject: [PATCH] memcg: Add a new sysctl parameter for automatically setting memory.high Date: Sun, 23 Jun 2024 16:45:14 -0400 Message-Id: <20240623204514.1032662-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2CEB7160005 X-Stat-Signature: 3nakryat7z457td87bcadnkc75oq4wws X-HE-Tag: 1719175557-339576 X-HE-Meta: U2FsdGVk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CilS0yQcwH/re4/16YZ9FYNhvLAcEl84ihVe927zuSGy3+V58N8BnNOYjk0Mvvtp4mk48g4tz8JpWgrGAGBGzy2LAc3JrKYU0YnJ0ZBmR7Y7YKScBzz6LrLIOHG/D7UbT5jesPf6CE9mUu/paW2MVPGxJW8+RkhRWKEwxjpl/ivllyBbTfFD9IyLn3l9JZ9SA0CG9gjGI09ceai0acPYmfGt64K6zUuHCqSnjCFCwNNuM1i2oD+YZSQ== 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: List-Subscribe: List-Unsubscribe: With memory cgroup v1, there is only a single "memory.limit_in_bytes" to be set to specify the maximum amount of memory that is allowed to be used. So a lot of memory cgroup using tools and applications allow users to specify a single memory limit. When they migrate to cgroup v2, they use the given memory limit to set memory.max and disregard memory.high for the time being. Without properly setting memory.high, these user space applications cannot make use of the memory cgroup v2 ability to further reduce the chance of OOM kills by throttling and early memory reclaim. This patch adds a new sysctl parameter "vm/memory_high_autoset_ratio" to enable setting "memory.high" automatically whenever "memory.max" is set as long as "memory.high" hasn't been explicitly set before. This will allow a system administrator or a middleware layer to greatly reduce the chance of memory cgroup OOM kills without worrying about how to properly set memory.high. The new sysctl parameter will allow a range of 0-100. The default value of 0 will disable memory.high auto setting. For any non-zero value "n", the actual ratio used will be "n/(n+1)". A user cannot set a fraction less than 1/2. Signed-off-by: Waiman Long Nacked-by: Michal Hocko --- Documentation/admin-guide/sysctl/vm.rst | 10 ++++++ include/linux/memcontrol.h | 3 ++ mm/memcontrol.c | 41 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/admin-guide/sysctl/vm.rst index e86c968a7a0e..250ec39dd5af 100644 --- a/Documentation/admin-guide/sysctl/vm.rst +++ b/Documentation/admin-guide/sysctl/vm.rst @@ -46,6 +46,7 @@ Currently, these files are in /proc/sys/vm: - mem_profiling (only if CONFIG_MEM_ALLOC_PROFILING=y) - memory_failure_early_kill - memory_failure_recovery +- memory_high_autoset_ratio - min_free_kbytes - min_slab_ratio - min_unmapped_ratio @@ -479,6 +480,15 @@ Enable memory failure recovery (when supported by the platform) 0: Always panic on a memory failure. +memory_high_autoset_ratio +========================= + +Specify a ratio by which memory.high should be set as a fraction of +memory.max if it hasn't been explicitly set before. It allows a range +of 0-100. The default value of 0 means auto setting will be disabled. +For any non-zero value "n", the actual ratio used will be "n/(n+1)". + + min_free_kbytes =============== diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 030d34e9d117..6be161a6b922 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -221,6 +221,9 @@ struct mem_cgroup { */ bool oom_group; + /* %true if memory.high has been explicitly set */ + bool memory_high_set; + /* protected by memcg_oom_lock */ bool oom_lock; int under_oom; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 71fe2a95b8bd..2cfb000bf543 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -6889,6 +6890,35 @@ static void mem_cgroup_attach(struct cgroup_taskset *tset) } #endif +/* + * The memory.high autoset ratio specifies a ratio by which memory.high + * should be set as a fraction of memory.max if it hasn't been explicitly + * set before. The default value of 0 means auto setting will be disabled. + * For any non-zero value "n", the actual ratio is "n/(n+1)". + */ +static int sysctl_memory_high_autoset_ratio; + +#ifdef CONFIG_SYSCTL +static struct ctl_table memcg_table[] = { + { + .procname = "memory_high_autoset_ratio", + .data = &sysctl_memory_high_autoset_ratio, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE_HUNDRED, + }, +}; + +static inline void memcg_sysctl_init(void) +{ + register_sysctl_init("vm", memcg_table); +} +#else +static void memcg_sysctl_init(void) { } +#endif /* CONFIG_SYSCTL */ + static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value) { if (value == PAGE_COUNTER_MAX) @@ -6982,6 +7012,7 @@ static ssize_t memory_high_write(struct kernfs_open_file *of, return err; page_counter_set_high(&memcg->memory, high); + memcg->memory_high_set = true; for (;;) { unsigned long nr_pages = page_counter_read(&memcg->memory); @@ -7023,6 +7054,7 @@ static ssize_t memory_max_write(struct kernfs_open_file *of, unsigned int nr_reclaims = MAX_RECLAIM_RETRIES; bool drained = false; unsigned long max; + unsigned int high_ratio = sysctl_memory_high_autoset_ratio; int err; buf = strstrip(buf); @@ -7032,6 +7064,13 @@ static ssize_t memory_max_write(struct kernfs_open_file *of, xchg(&memcg->memory.max, max); + if (high_ratio && !memcg->memory_high_set) { + /* Set memory.high as a fraction of memory.max */ + unsigned long high = max * high_ratio / (high_ratio + 1); + + page_counter_set_high(&memcg->memory, high); + } + for (;;) { unsigned long nr_pages = page_counter_read(&memcg->memory); @@ -7977,6 +8016,8 @@ static int __init mem_cgroup_init(void) soft_limit_tree.rb_tree_per_node[node] = rtpn; } + memcg_sysctl_init(); + return 0; } subsys_initcall(mem_cgroup_init);