From nobody Tue Dec 16 19:40:04 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1A9A1DFF8 for ; Sun, 23 Jun 2024 20:45:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719175558; cv=none; b=pwN8W5P8DiuL2sxbkRvIFJSmOrGwBTOUdbta4S89LuY2rldX38meBxoGjatsinaYk63jI+R/pvyjsZNpzEyr7H1CMk2z6v6El1khumlLZO7jUo6rY9gFgIPv+DlNSAIy8u3rJbZuhAoR2E/HBIARa1hRnSGCH32FW1u52RKJTPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719175558; c=relaxed/simple; bh=DPvRmvVmh2WgSV6K/Scm1uUzCJnRcqLxH5aT6E+qO74=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=CxZDiKjurAYtEDoUmbe30b0Xfp4MS2+HCtnint1ex6wlYzqdlufe9HEZQk0JmH94I5/Nycdy2sa68Nw28Aa7r4NMjr5d4mHTv1E9aSCfZ3U/uTJMYNX0bqyLrT694uIiFOGXE3mRgGYnp6kUjDQRhvwtC2qIHrcM2bk4gSGZL2g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=D1SpNnXw; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="D1SpNnXw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719175554; 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=D1SpNnXwtzq9e2VMrzuxm3SvKR6h1D2UqM3wDnxJkubbYyu/ZdXNQRHLkwYKxykzyPAN7T H8rJlmcU4Ot/A4JaJ/eUX/awa8qAaucp9WVFeB1ACq3IaD5eF7c4cl+yzIs8fMOkcgJMYq 4Kz82bogQoqy6x1OW3mY6AYF/J/wf40= 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" 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 --- 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=3Dy) - 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. =20 =20 +memory_high_autoset_ratio +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + +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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 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; =20 + /* %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 =20 +/* + * 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[] =3D { + { + .procname =3D "memory_high_autoset_ratio", + .data =3D &sysctl_memory_high_autoset_ratio, + .maxlen =3D sizeof(int), + .mode =3D 0644, + .proc_handler =3D proc_dointvec_minmax, + .extra1 =3D SYSCTL_ZERO, + .extra2 =3D 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 =3D=3D PAGE_COUNTER_MAX) @@ -6982,6 +7012,7 @@ static ssize_t memory_high_write(struct kernfs_open_f= ile *of, return err; =20 page_counter_set_high(&memcg->memory, high); + memcg->memory_high_set =3D true; =20 for (;;) { unsigned long nr_pages =3D page_counter_read(&memcg->memory); @@ -7023,6 +7054,7 @@ static ssize_t memory_max_write(struct kernfs_open_fi= le *of, unsigned int nr_reclaims =3D MAX_RECLAIM_RETRIES; bool drained =3D false; unsigned long max; + unsigned int high_ratio =3D sysctl_memory_high_autoset_ratio; int err; =20 buf =3D strstrip(buf); @@ -7032,6 +7064,13 @@ static ssize_t memory_max_write(struct kernfs_open_f= ile *of, =20 xchg(&memcg->memory.max, max); =20 + if (high_ratio && !memcg->memory_high_set) { + /* Set memory.high as a fraction of memory.max */ + unsigned long high =3D max * high_ratio / (high_ratio + 1); + + page_counter_set_high(&memcg->memory, high); + } + for (;;) { unsigned long nr_pages =3D page_counter_read(&memcg->memory); =20 @@ -7977,6 +8016,8 @@ static int __init mem_cgroup_init(void) soft_limit_tree.rb_tree_per_node[node] =3D rtpn; } =20 + memcg_sysctl_init(); + return 0; } subsys_initcall(mem_cgroup_init); --=20 2.39.3