From patchwork Wed Apr 5 11:01:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaun Tancheff X-Patchwork-Id: 13201615 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 7D51EC76188 for ; Wed, 5 Apr 2023 11:09:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AAA7B6B0071; Wed, 5 Apr 2023 07:09:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A3CFA6B0072; Wed, 5 Apr 2023 07:09:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D44C6B0074; Wed, 5 Apr 2023 07:09:47 -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 7ABD16B0071 for ; Wed, 5 Apr 2023 07:09:47 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 49FB61211F5 for ; Wed, 5 Apr 2023 11:09:47 +0000 (UTC) X-FDA: 80647067214.28.DC2BFCD Received: from mail-il1-f171.google.com (mail-il1-f171.google.com [209.85.166.171]) by imf04.hostedemail.com (Postfix) with ESMTP id 730E74000E for ; Wed, 5 Apr 2023 11:09:45 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=eRfGVFQs; spf=pass (imf04.hostedemail.com: domain of shaun.tancheff@gmail.com designates 209.85.166.171 as permitted sender) smtp.mailfrom=shaun.tancheff@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680692985; a=rsa-sha256; cv=none; b=ip5YOGgWaj1nRfYjAn1iAqzJXoUWSnx0O9OUtTJ50MAXlm/JKWVxLdc2CzzvQTPRE1MU/K rAbi7lAJf9KWYrnpAMQwZQW+BX9XqWlDkYw5HK6P5H5qlQ4bUL98bzG7HKEZruztHzGgfX YE9oXMS6mQ5b0I7puLm/ijX5hemH9aE= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=eRfGVFQs; spf=pass (imf04.hostedemail.com: domain of shaun.tancheff@gmail.com designates 209.85.166.171 as permitted sender) smtp.mailfrom=shaun.tancheff@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680692985; 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=iJGAMvCER1Maz71WC4+d0+uJtDiWgp7hdUA5ioSAboA=; b=CoyugFI2oMn14wdJlAEaLNhYpHQxolavu4Z3Hon++CWSOJs4pNceXNOmWSp5HcrZbuX7GV fyzkQaZxousyZxZhgNcWSu8Wg4FeKD8iHlDgKZeu5bYPs4pqwC9tXo8sROT8iDBJY5g4dv jJL9tTc+P7j4PKrkua1vXTANJ6VSdMY= Received: by mail-il1-f171.google.com with SMTP id j19so1884855ilq.9 for ; Wed, 05 Apr 2023 04:09:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680692984; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=iJGAMvCER1Maz71WC4+d0+uJtDiWgp7hdUA5ioSAboA=; b=eRfGVFQsBvRXIlEWlxf38DifSy7tG1irsLh1En2kCHb88kljQ+VjtvyZGJ/UHYeCF3 JZngb/qYwpcMGGb2qToz44biwDK1i8igFbmOZyV+IVn+urJuAJJtgmUVjY9KczqBjIcx PwaCB38enXdEHKxhNgUoyTuGv8wdxKlvwzliNIjFXF/WT7tF8ngHv8lDHohb1xgfFiry QuT+ChseqOvsGjSAZSjXi6NY/Zah2ntos7IBZIGN1fRpYRcrnQ36H+kp+ThiSevoastR zDHD+CPEd1IfptRamronzFDB/4vZZPkZl0RjNusLYXubJNAixF1PPgPWtc4yu6bx7a+T H43A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680692984; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=iJGAMvCER1Maz71WC4+d0+uJtDiWgp7hdUA5ioSAboA=; b=1ptXaunhhYLQE3YTzxTcYfqUCNL7WXZ7okQZTMfcWLsQpLsPQsENj2CcBkEnLqIu7g BWnHpm7sM43Ucsf7u31yWOWfA3LsT1yf+77C76feC9U4Y84lKaY6rnCOEl75lLHzu0Bb 5/yM6MW3O+IrvsW0U0H7Hi8E3syZSurVHefbgf+Txui2+HN7Y2djQt7fcGg/BsNKuyL3 uFFD+rSI1P/iWX6eL5TZ99RW7khPVTerkFO+F6IzRMqtCMppZsRna/QdhY93OT+9K64F 7vN3iZpO4s4ctHJRx1LdmJBeFiwRUfSnCD6Qf93dA5XbC1rTP+p6CQmV9x8DV4HxOy2u 4mLQ== X-Gm-Message-State: AAQBX9f2ytbRwrhF/NJ/wL14mOWCGB4TWLHCBnEiss4DynMVT935JHw1 8VgrmT0b7B8qVMA0W7uxN8Bxod0YiAyUO0Dp X-Google-Smtp-Source: AKy350YwlKoCBpiBymdY26Utiux0aRqLnTsThq8o6n5gNgRGHnQlVHaic+BuLQMT7CE0kVxzyTniAQ== X-Received: by 2002:a17:90b:1b09:b0:237:f925:f63 with SMTP id nu9-20020a17090b1b0900b00237f9250f63mr6383415pjb.13.1680692484650; Wed, 05 Apr 2023 04:01:24 -0700 (PDT) Received: from lunar.aeonazure.com ([182.2.143.216]) by smtp.gmail.com with ESMTPSA id f11-20020a17090274cb00b001a065d3bb0esm9831373plt.211.2023.04.05.04.01.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 04:01:24 -0700 (PDT) From: Shaun Tancheff To: Johannes Weiner , Michal Hocko , Vladimir Davydov Cc: Shaun Tancheff , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH] memcg-v1: Enable setting memory min, low, high Date: Wed, 5 Apr 2023 18:01:07 +0700 Message-Id: <20230405110107.127156-1-shaun.tancheff@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 730E74000E X-Rspamd-Server: rspam01 X-Stat-Signature: xms3pan5s8rjbnz8srdhmtr17s94jc8c X-HE-Tag: 1680692985-901857 X-HE-Meta: U2FsdGVkX18VCq0EzJ5L6WED9Qdms0gcuUAsLWVwxdyHfQq6twcqEAffTfBz1/j9Ms5ZRhkUFXpRNo8gJiY+Q80KTfAiPVt+/T2kbD1wOYpPrSBLa/GM9p0+Yh5Zy9ikC8zRpwUzGB4ZxeAMIQdDInfUBLlufMmLUHsU1p9XLRFjCs7KByixW3vMzus/ydCFEiLO2bJIQ7aijayeOOAxT4TtIV1D7KvLY1nYZGSYNmyUX7Y1bJK3S3Q0R9On4qjFwM+3US2ju4HUOKQu2EjKBItitx94NBFkzCEjAIRJ6A+Ft2mdjimZDhJY5xoobw4w7c0uCZqoga9GqxhxFzCad859JQmDHiFCw3x7Hs5Ya4wIe7IDYvNL1YZMgu38zUsEGPh/fFg4FfbeFtc+dTebGzoWS/JLkVyIjCzdgM/l2Eh84nAr/yv9EOmXA7MC2Ih+za6lwf/VZ8HQz/aUZtOGiNa4dKkWh1Q4xKiG4ONhLYENYPW5RYPdzG9NYW170AD4sZ96iuK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jwf 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: From: Shaun Tancheff For users that are unable to update to memcg-v2 this provides a method where memcg-v1 can more effectively apply enough memory pressure to effectively throttle filesystem I/O or otherwise minimize being memcg oom killed at the expense of reduced performance. This patch extends the memcg-v1 legacy sysfs entries with: limit_in_bytes.min, limit_in_bytes.low and limit_in_bytes.high Since old software will need to be updated to take advantage of the new files a secondary method of setting min, low and high based on a percentage of the limit is also provided. The percentages are determined by module parameters. The available module parameters can be set at kernel boot time, for example: memcontrol.memcg_min=10 memcontrol.memcg_low=30 memcontrol.memcg_high=80 Would set min to 10%, low to 30% and high to 80% of the value written to: /sys/fs/cgroup/memory//memory.limit_in_bytes Signed-off-by: Shaun Tancheff --- v0: Initial hard coded limits by percent. v1: Added sysfs access and module parameters for percent values to enable v2: Fix 32-bit, remove need for missing __udivdi3 mm/memcontrol.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 2eee092f8f11..3cf8386f4f45 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -73,6 +73,18 @@ #include +static unsigned int memcg_v1_min_default_percent; +module_param_named(memcg_min, memcg_v1_min_default_percent, uint, 0600); +MODULE_PARM_DESC(memcg_min, "memcg v1 min default percent"); + +static unsigned int memcg_v1_low_default_percent; +module_param_named(memcg_low, memcg_v1_low_default_percent, uint, 0600); +MODULE_PARM_DESC(memcg_low, "memcg v1 low default percent"); + +static unsigned int memcg_v1_high_default_percent; +module_param_named(memcg_high, memcg_v1_high_default_percent, uint, 0600); +MODULE_PARM_DESC(memcg_high, "memcg v1 high default percent"); + struct cgroup_subsys memory_cgrp_subsys __read_mostly; EXPORT_SYMBOL(memory_cgrp_subsys); @@ -205,6 +217,7 @@ enum res_type { _MEMSWAP, _KMEM, _TCP, + _MEM_V1, }; #define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val)) @@ -3676,6 +3689,9 @@ enum { RES_MAX_USAGE, RES_FAILCNT, RES_SOFT_LIMIT, + RES_LIMIT_MIN, + RES_LIMIT_LOW, + RES_LIMIT_HIGH, }; static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css, @@ -3686,6 +3702,7 @@ static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css, switch (MEMFILE_TYPE(cft->private)) { case _MEM: + case _MEM_V1: counter = &memcg->memory; break; case _MEMSWAP: @@ -3716,6 +3733,12 @@ static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css, return counter->failcnt; case RES_SOFT_LIMIT: return (u64)memcg->soft_limit * PAGE_SIZE; + case RES_LIMIT_MIN: + return (u64)READ_ONCE(memcg->memory.min); + case RES_LIMIT_LOW: + return (u64)READ_ONCE(memcg->memory.low); + case RES_LIMIT_HIGH: + return (u64)READ_ONCE(memcg->memory.high); default: BUG(); } @@ -3815,6 +3838,34 @@ static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max) return ret; } +static inline void mem_cgroup_v1_set_defaults(struct mem_cgroup *memcg, + unsigned long nr_pages) +{ + unsigned long min, low, high; + + if (mem_cgroup_is_root(memcg) || PAGE_COUNTER_MAX == nr_pages) + return; + + min = READ_ONCE(memcg->memory.min); + low = READ_ONCE(memcg->memory.low); + if (min || low) + return; + + if (!min && memcg_v1_min_default_percent) { + min = (nr_pages * memcg_v1_min_default_percent) / 100; + page_counter_set_min(&memcg->memory, min); + } + if (!low && memcg_v1_low_default_percent) { + low = (nr_pages * memcg_v1_low_default_percent) / 100; + page_counter_set_low(&memcg->memory, low); + } + high = READ_ONCE(memcg->memory.high); + if (high == PAGE_COUNTER_MAX && memcg_v1_high_default_percent) { + high = (nr_pages * memcg_v1_high_default_percent) / 100; + page_counter_set_high(&memcg->memory, high); + } +} + /* * The user of this function is... * RES_LIMIT. @@ -3838,6 +3889,11 @@ static ssize_t mem_cgroup_write(struct kernfs_open_file *of, break; } switch (MEMFILE_TYPE(of_cft(of)->private)) { + case _MEM_V1: + ret = mem_cgroup_resize_max(memcg, nr_pages, false); + if (!ret) + mem_cgroup_v1_set_defaults(memcg, nr_pages); + break; case _MEM: ret = mem_cgroup_resize_max(memcg, nr_pages, false); break; @@ -4986,6 +5042,13 @@ static int mem_cgroup_slab_show(struct seq_file *m, void *p) } #endif +static ssize_t memory_min_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off); +static ssize_t memory_low_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off); +static ssize_t memory_high_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off); + static struct cftype mem_cgroup_legacy_files[] = { { .name = "usage_in_bytes", @@ -5000,10 +5063,28 @@ static struct cftype mem_cgroup_legacy_files[] = { }, { .name = "limit_in_bytes", - .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), + .private = MEMFILE_PRIVATE(_MEM_V1, RES_LIMIT), .write = mem_cgroup_write, .read_u64 = mem_cgroup_read_u64, }, + { + .name = "limit_in_bytes.min", + .private = MEMFILE_PRIVATE(_MEM_V1, RES_LIMIT_MIN), + .write = memory_min_write, + .read_u64 = mem_cgroup_read_u64, + }, + { + .name = "limit_in_bytes.low", + .private = MEMFILE_PRIVATE(_MEM_V1, RES_LIMIT_LOW), + .write = memory_low_write, + .read_u64 = mem_cgroup_read_u64, + }, + { + .name = "limit_in_bytes.high", + .private = MEMFILE_PRIVATE(_MEM_V1, RES_LIMIT_HIGH), + .write = memory_high_write, + .read_u64 = mem_cgroup_read_u64, + }, { .name = "soft_limit_in_bytes", .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),