From nobody Sun May 10 19:52:30 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5B47C433EF for ; Mon, 25 Apr 2022 19:01:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244806AbiDYTEQ (ORCPT ); Mon, 25 Apr 2022 15:04:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244801AbiDYTED (ORCPT ); Mon, 25 Apr 2022 15:04:03 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E63EAE41FE for ; Mon, 25 Apr 2022 12:00:55 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id gg5-20020a17090b0a0500b001d9852bd129so44683pjb.9 for ; Mon, 25 Apr 2022 12:00:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=fBFNWoBuZ+rUFYDa2D6wGphqTYvQjamUdIjSsw6TIq0=; b=smHSwP6GCAdbnpTWe49B9oKF0iYhl1bzB1K3BDIn9UhpSm9W7gWtmt2DRS6JQKMy8o BajDe1FI0jRx+0D1XfBG9Tjf8xi8/2gRrmgadMgDjdPr01tIohPi8twnF3DK6BGgIVvi 3yOdRTAnVNgeawrn30eQ9SYwTdBmFzJ6tY5eY0W6C6U4q9dlHlnFVJU6QSb8bqBoq2P5 R85Z1HAt6KgkwQKubfSsF9s5YBGQN4nGEmwB9Jg3JkRFLErxcvUGrbweooRE4a+KXyK+ F2xMNNMm5DykM7dmSOdrjgcVgow14TDKqX495S1XlPEkUL5iomDIlLABrbbmXI8Ja1Oe JrIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=fBFNWoBuZ+rUFYDa2D6wGphqTYvQjamUdIjSsw6TIq0=; b=CdDj/mfflrKTDCtTq94sfjs5eNltkrbb8T7iX6pUuUl4cBEX1/PBSBNI1LMq/5FKrU bsehelgQXICwoEsv56AwEKcxjRZkBJ2jgjYKGeTmIQvHelo15RU/qWMBVOiz0NxlNG0w gV7zZIdUJYxAg/uw28PvKC9BdlCMPdB9uj2ZoXFX8s/1X4daWkfsZVzhwXQ2Z8s8GULq ZHLniVx4hnth6TcNsWpUkMcV2Ektr1/FT+PHGqswLC/bOA+FfqM59wgoLwKq//8sTbIY ijEWLwiK7qXhgSi0EPhXkbfU6ueypjw8LFAycs+rEXtKHi5aXqTt1AJfZ9g8QjZmlduV ma3w== X-Gm-Message-State: AOAM530SfYTQkFXoYBE6fECCMMYSVYluoey0bmzYRiUdxkEnyYDJ8aIG giHEFIPW9ZIE7WEgTKH/agXfjvJePnTDdd2R X-Google-Smtp-Source: ABdhPJxt8xE4NWnQfnST2T2fwscg58NT92N6YrUwlKz1bvN3nvLWPKcU8F4b6X/rtWS2BqN9NjKAgFqXj2Zk/cq6 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a65:6e88:0:b0:39d:2647:f75d with SMTP id bm8-20020a656e88000000b0039d2647f75dmr16444204pgb.523.1650913255418; Mon, 25 Apr 2022 12:00:55 -0700 (PDT) Date: Mon, 25 Apr 2022 19:00:37 +0000 In-Reply-To: <20220425190040.2475377-1-yosryahmed@google.com> Message-Id: <20220425190040.2475377-2-yosryahmed@google.com> Mime-Version: 1.0 References: <20220425190040.2475377-1-yosryahmed@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v5 1/4] memcg: introduce per-memcg reclaim interface From: Yosry Ahmed To: Johannes Weiner , Michal Hocko , Shakeel Butt , Andrew Morton , Roman Gushchin Cc: David Rientjes , Tejun Heo , Zefan Li , Jonathan Corbet , Shuah Khan , Yu Zhao , Dave Hansen , Wei Xu , Greg Thelen , Chen Wandun , Vaibhav Jain , "=?UTF-8?q?Michal=20Koutn=C3=BD?=" , Tim Chen , cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Yosry Ahmed , Michal Hocko Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Shakeel Butt Introduce a memcg interface to trigger memory reclaim on a memory cgroup. Use case: Proactive Reclaim Acked-by: David Rientjes Acked-by: Johannes Weiner Acked-by: Michal Hocko Acked-by: Roman Gushchin Acked-by: Wei Xu Reviewed-by: Davidlohr Bueso --------------------------- A userspace proactive reclaimer can continuously probe the memcg to reclaim a small amount of memory. This gives more accurate and up-to-date workingset estimation as the LRUs are continuously sorted and can potentially provide more deterministic memory overcommit behavior. The memory overcommit controller can provide more proactive response to the changing behavior of the running applications instead of being reactive. A userspace reclaimer's purpose in this case is not a complete replacement for kswapd or direct reclaim, it is to proactively identify memory savings opportunities and reclaim some amount of cold pages set by the policy to free up the memory for more demanding jobs or scheduling new jobs. A user space proactive reclaimer is used in Google data centers. Additionally, Meta's TMO paper recently referenced a very similar interface used for user space proactive reclaim: https://dl.acm.org/doi/pdf/10.1145/3503222.3507731 Benefits of a user space reclaimer: ----------------------------------- 1) More flexible on who should be charged for the cpu of the memory reclaim. For proactive reclaim, it makes more sense to be centralized. 2) More flexible on dedicating the resources (like cpu). The memory overcommit controller can balance the cost between the cpu usage and the memory reclaimed. 3) Provides a way to the applications to keep their LRUs sorted, so, under memory pressure better reclaim candidates are selected. This also gives more accurate and uptodate notion of working set for an application. Why memory.high is not enough? ------------------------------ - memory.high can be used to trigger reclaim in a memcg and can potentially be used for proactive reclaim. However there is a big downside in using memory.high. It can potentially introduce high reclaim stalls in the target application as the allocations from the processes or the threads of the application can hit the temporary memory.high limit. - Userspace proactive reclaimers usually use feedback loops to decide how much memory to proactively reclaim from a workload. The metrics used for this are usually either refaults or PSI, and these metrics will become messy if the application gets throttled by hitting the high limit. - memory.high is a stateful interface, if the userspace proactive reclaimer crashes for any reason while triggering reclaim it can leave the application in a bad state. - If a workload is rapidly expanding, setting memory.high to proactively reclaim memory can result in actually reclaiming more memory than intended. The benefits of such interface and shortcomings of existing interface were further discussed in this RFC thread: https://lore.kernel.org/linux-mm/5df21376-7dd1-bf81-8414-32a73cea45dd@googl= e.com/ Interface: ---------- Introducing a very simple memcg interface 'echo 10M > memory.reclaim' to trigger reclaim in the target memory cgroup. The interface is introduced as a nested-keyed file to allow for future optional arguments to be easily added to configure the behavior of reclaim. Possible Extensions: -------------------- - This interface can be extended with an additional parameter or flags to allow specifying one or more types of memory to reclaim from (e.g. file, anon, ..). - The interface can also be extended with a node mask to reclaim from specific nodes. This has use cases for reclaim-based demotion in memory tiering systens. - A similar per-node interface can also be added to support proactive reclaim and reclaim-based demotion in systems without memcg. - Add a timeout parameter to make it easier for user space to call the interface without worrying about being blocked for an undefined amount of time. For now, let's keep things simple by adding the basic functionality. [yosryahmed@google.com: worked on versions v2 onwards, refreshed to current master, updated commit message based on recent discussions and use cases] Signed-off-by: Shakeel Butt Co-developed-by: Yosry Ahmed Signed-off-by: Yosry Ahmed Acked-by: Johannes Weiner Acked-by: Michal Hocko Acked-by: Wei Xu Acked-by: Roman Gushchin --- Documentation/admin-guide/cgroup-v2.rst | 21 ++++++++++++ mm/memcontrol.c | 45 +++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-= guide/cgroup-v2.rst index 69d7a6983f78..19bcd73cad03 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1208,6 +1208,27 @@ PAGE_SIZE multiple when read back. high limit is used and monitored properly, this limit's utility is limited to providing the final safety net. =20 + memory.reclaim + A write-only nested-keyed file which exists for all cgroups. + + This is a simple interface to trigger memory reclaim in the + target cgroup. + + This file accepts a single key, the number of bytes to reclaim. + No nested keys are currently supported. + + Example:: + + echo "1G" > memory.reclaim + + The interface can be later extended with nested keys to + configure the reclaim behavior. For example, specify the + type of memory to reclaim from (anon, file, ..). + + Please note that the kernel can over or under reclaim from + the target cgroup. If less bytes are reclaimed than the + specified amount, -EAGAIN is returned. + memory.oom.group A read-write single value file which exists on non-root cgroups. The default value is "0". diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 725f76723220..879f1716d6e9 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6355,6 +6355,46 @@ static ssize_t memory_oom_group_write(struct kernfs_= open_file *of, return nbytes; } =20 +static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, + size_t nbytes, loff_t off) +{ + struct mem_cgroup *memcg =3D mem_cgroup_from_css(of_css(of)); + unsigned int nr_retries =3D MAX_RECLAIM_RETRIES; + unsigned long nr_to_reclaim, nr_reclaimed =3D 0; + int err; + + buf =3D strstrip(buf); + err =3D page_counter_memparse(buf, "", &nr_to_reclaim); + if (err) + return err; + + while (nr_reclaimed < nr_to_reclaim) { + unsigned long reclaimed; + + if (signal_pending(current)) + return -EINTR; + + /* + * This is the final attempt, drain percpu lru caches in the + * hope of introducing more evictable pages for + * try_to_free_mem_cgroup_pages(). + */ + if (!nr_retries) + lru_add_drain_all(); + + reclaimed =3D try_to_free_mem_cgroup_pages(memcg, + nr_to_reclaim - nr_reclaimed, + GFP_KERNEL, true); + + if (!reclaimed && !nr_retries--) + return -EAGAIN; + + nr_reclaimed +=3D reclaimed; + } + + return nbytes; +} + static struct cftype memory_files[] =3D { { .name =3D "current", @@ -6413,6 +6453,11 @@ static struct cftype memory_files[] =3D { .seq_show =3D memory_oom_group_show, .write =3D memory_oom_group_write, }, + { + .name =3D "reclaim", + .flags =3D CFTYPE_NS_DELEGATABLE, + .write =3D memory_reclaim, + }, { } /* terminate */ }; =20 --=20 2.36.0.rc2.479.g8af0fa9b8e-goog From nobody Sun May 10 19:52:30 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EEB66C433F5 for ; Mon, 25 Apr 2022 19:01:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244802AbiDYTEL (ORCPT ); Mon, 25 Apr 2022 15:04:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244805AbiDYTED (ORCPT ); Mon, 25 Apr 2022 15:04:03 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07372E5E15 for ; Mon, 25 Apr 2022 12:00:58 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id z5-20020a170902ccc500b0015716eaec65so10001099ple.14 for ; Mon, 25 Apr 2022 12:00:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=OERxjUiMWr1cQdsDG2DOpxyattqqP1OOWWgJQWH3L+Q=; b=jny3uMCrVBnmU4KDEBjxFsNJ+VQFDuZs2Of3Od5sz8dt2N8StM4QwzqkfKRfsR2PfJ 04xxC8sY4WVBXUNIHQnO3TDQtNk2mQI5Bv9TrMkGE46T+NAgqKUPQzVfhj1gB8tDph5u UmK+li0N/cdU24sX+whw1L8S1DSaDJ4Y+wD/WlGONCStk3DPn7EmkqUT0OgZ8aYJ6dyn D5vmzhn/g+i+rRip2IlvPQOMq/Yv8PFURN8S3CPi5j868Dex+B48+XwMpbVAMJy/W7AT cJzzCERjoZDLjEcEDQyUGJOSErR614eYQY7PZP9qDV4G7CSk3yjVAqmJHqS+FU2WYbGN gDwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=OERxjUiMWr1cQdsDG2DOpxyattqqP1OOWWgJQWH3L+Q=; b=1nCWm+7ncwmY42dlMiAmXhWN4kk8En1pylVYsk2DJhrexlxgNeg49MzBZYPj2GxZSu VQsy0u34WIBe6rK+vH6RYJkzd+GZXhiHOC2l9X50x+cuZXsqnseDrN/tQYngV/E5Kk4h 4wfIKrbYNFwqb8egFamYGE2WncjRC455WrTIhBHZipyatn9OW/y20NLRD++RMP0i/olR 3z/Y5KorUgdvUIPYWmkSTsVwn1kb+HPOElpct+5y8hyebOfeG3aQ6b9A9uqdUJ/pRIbS DbIAJeFLdeZ6p6QrvzVnxpF0ZeV9tbhXZqctg0f8jrrlZrwuBzDzUlX+QN0tdQr0vsrH aA1Q== X-Gm-Message-State: AOAM533FRS89ZbO9TTnc3Q/4+hAUZj67yZ8u1oDPiY6okMuk8ICvR6AH G+cCfwo4tAYHUAWX5hvF8L+AIFOpJwfpwADR X-Google-Smtp-Source: ABdhPJyuLEwI6VGaaT+2zi+YCPqvG9+iIGmxhIFRCLcY9IfcS41UvuhS+EBApDVh5mp5ySwJen6WcU1Wg3Nk6iDY X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a17:90a:c986:b0:1d9:56e7:4e83 with SMTP id w6-20020a17090ac98600b001d956e74e83mr693654pjt.1.1650913256918; Mon, 25 Apr 2022 12:00:56 -0700 (PDT) Date: Mon, 25 Apr 2022 19:00:38 +0000 In-Reply-To: <20220425190040.2475377-1-yosryahmed@google.com> Message-Id: <20220425190040.2475377-3-yosryahmed@google.com> Mime-Version: 1.0 References: <20220425190040.2475377-1-yosryahmed@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v5 2/4] selftests: cgroup: return -errno from cg_read()/cg_write() on failure From: Yosry Ahmed To: Johannes Weiner , Michal Hocko , Shakeel Butt , Andrew Morton , Roman Gushchin Cc: David Rientjes , Tejun Heo , Zefan Li , Jonathan Corbet , Shuah Khan , Yu Zhao , Dave Hansen , Wei Xu , Greg Thelen , Chen Wandun , Vaibhav Jain , "=?UTF-8?q?Michal=20Koutn=C3=BD?=" , Tim Chen , cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Yosry Ahmed Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, cg_read()/cg_write() returns 0 on success and -1 on failure. Modify them to return the -errno on failure. Signed-off-by: Yosry Ahmed Acked-by: Shakeel Butt Acked-by: David Rientjes Acked-by: Roman Gushchin --- tools/testing/selftests/cgroup/cgroup_util.c | 44 +++++++++----------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/s= elftests/cgroup/cgroup_util.c index dbaa7aabbb4a..e6f3679cdcc0 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.c +++ b/tools/testing/selftests/cgroup/cgroup_util.c @@ -19,6 +19,7 @@ #include "cgroup_util.h" #include "../clone3/clone3_selftests.h" =20 +/* Returns read len on success, or -errno on failure. */ static ssize_t read_text(const char *path, char *buf, size_t max_len) { ssize_t len; @@ -26,35 +27,29 @@ static ssize_t read_text(const char *path, char *buf, s= ize_t max_len) =20 fd =3D open(path, O_RDONLY); if (fd < 0) - return fd; + return -errno; =20 len =3D read(fd, buf, max_len - 1); - if (len < 0) - goto out; =20 - buf[len] =3D 0; -out: + if (len >=3D 0) + buf[len] =3D 0; + close(fd); - return len; + return len < 0 ? -errno : len; } =20 +/* Returns written len on success, or -errno on failure. */ static ssize_t write_text(const char *path, char *buf, ssize_t len) { int fd; =20 fd =3D open(path, O_WRONLY | O_APPEND); if (fd < 0) - return fd; + return -errno; =20 len =3D write(fd, buf, len); - if (len < 0) { - close(fd); - return len; - } - close(fd); - - return len; + return len < 0 ? -errno : len; } =20 char *cg_name(const char *root, const char *name) @@ -87,16 +82,16 @@ char *cg_control(const char *cgroup, const char *contro= l) return ret; } =20 +/* Returns 0 on success, or -errno on failure. */ int cg_read(const char *cgroup, const char *control, char *buf, size_t len) { char path[PATH_MAX]; + ssize_t ret; =20 snprintf(path, sizeof(path), "%s/%s", cgroup, control); =20 - if (read_text(path, buf, len) >=3D 0) - return 0; - - return -1; + ret =3D read_text(path, buf, len); + return ret >=3D 0 ? 0 : ret; } =20 int cg_read_strcmp(const char *cgroup, const char *control, @@ -177,17 +172,15 @@ long cg_read_lc(const char *cgroup, const char *contr= ol) return cnt; } =20 +/* Returns 0 on success, or -errno on failure. */ int cg_write(const char *cgroup, const char *control, char *buf) { char path[PATH_MAX]; - ssize_t len =3D strlen(buf); + ssize_t len =3D strlen(buf), ret; =20 snprintf(path, sizeof(path), "%s/%s", cgroup, control); - - if (write_text(path, buf, len) =3D=3D len) - return 0; - - return -1; + ret =3D write_text(path, buf, len); + return ret =3D=3D len ? 0 : ret; } =20 int cg_find_unified_root(char *root, size_t len) @@ -545,7 +538,8 @@ ssize_t proc_read_text(int pid, bool thread, const char= *item, char *buf, size_t else snprintf(path, sizeof(path), "/proc/%d/%s", pid, item); =20 - return read_text(path, buf, size); + size =3D read_text(path, buf, size); + return size < 0 ? -1 : size; } =20 int proc_read_strstr(int pid, bool thread, const char *item, const char *n= eedle) --=20 2.36.0.rc2.479.g8af0fa9b8e-goog From nobody Sun May 10 19:52:30 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D82ECC433EF for ; Mon, 25 Apr 2022 19:01:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244778AbiDYTEH (ORCPT ); Mon, 25 Apr 2022 15:04:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244772AbiDYTEE (ORCPT ); Mon, 25 Apr 2022 15:04:04 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74D0B44740 for ; Mon, 25 Apr 2022 12:00:59 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id m12-20020a17090a71cc00b001d692bcbae1so1484211pjs.5 for ; Mon, 25 Apr 2022 12:00:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=eqHPhXVFsY1aWd5kzuU1eL2wsGrZ6tPFgC+x7Ju6qxo=; b=PBg+H68ojw1koOQnEJrZ6MMtj1acWkPTyAwFIkSY0wqW+D0Ubjw5x2Ksetg2ivOCuZ 5+tPJvp8xXIWyTgLtC+OXULAzhoJB2tO+3NY5OZB2VK0H+IoAxoJ8VoDcAmflY6BOnuZ drcAFgQM1a/JXp2LFIgumdKBshtSyBa72bu7b+fT/jUHqCsFpJwyLx4jLvX1rBMK8Z4s SzNgZFO9Pt8mEDE9EBL/QcCwmtAnQ/dCvFYOcWA5AQ4H3e0AstKYC+NIlkg1m6J2jkIK +O74AyMLPo8rXFTpkOOt9jzBOKCtuVYM0QLUxRJQhzNOwiQgVR+/6fI4u/oBy+9Lpov5 1sZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=eqHPhXVFsY1aWd5kzuU1eL2wsGrZ6tPFgC+x7Ju6qxo=; b=AKOs3d3QZ+xlGLxFaxch3z3QLgE7i7pYNDSHr9oaKsROJRFOxM34dC7EA3vNuvAlaE cuiQJxlAUE776qHUJkmitvSuSzcIQGwq28JiLiVjKaaETaprD0Rn/YZWSleREnRjtyMI 7iHrPwSievxJ6ug3vgBkNF/DV5JRoucKiDduIE3tEYOFdtSKN9TfhWF/ChpF/JFnBvys 8q10BQkAxfsXXWWKjWeZYuYJha6MwNhO0hc/pl+AoDIPli48WPh80HDppW1g9AAuBTMT Srjpji9/fdD/5NKXoYT4GEx0fcUlFwgU0u+e7x9i4tF14j/2IQuwiaHg7XUph4IlRe6F TRtA== X-Gm-Message-State: AOAM531P6rUS5Xkb3df5OA5jk0xkjaFz/93WiWwWOjiuwuUaNFX1rccX CEo1eQA6AJjTGpAP5e8Y1ow38ibRNE/sxla6 X-Google-Smtp-Source: ABdhPJx21kD4fs+Pmg4O/2ap8I01dUttJqH2BxaSjv6wllmIYa+0VJzGMqxovlYs0K2IGdPVqKy9drNg7BmOd4FK X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a65:4c8e:0:b0:3aa:24bf:9e63 with SMTP id m14-20020a654c8e000000b003aa24bf9e63mr16510838pgt.592.1650913258801; Mon, 25 Apr 2022 12:00:58 -0700 (PDT) Date: Mon, 25 Apr 2022 19:00:39 +0000 In-Reply-To: <20220425190040.2475377-1-yosryahmed@google.com> Message-Id: <20220425190040.2475377-4-yosryahmed@google.com> Mime-Version: 1.0 References: <20220425190040.2475377-1-yosryahmed@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v5 3/4] selftests: cgroup: fix alloc_anon_noexit() instantly freeing memory From: Yosry Ahmed To: Johannes Weiner , Michal Hocko , Shakeel Butt , Andrew Morton , Roman Gushchin Cc: David Rientjes , Tejun Heo , Zefan Li , Jonathan Corbet , Shuah Khan , Yu Zhao , Dave Hansen , Wei Xu , Greg Thelen , Chen Wandun , Vaibhav Jain , "=?UTF-8?q?Michal=20Koutn=C3=BD?=" , Tim Chen , cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Yosry Ahmed Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, alloc_anon_noexit() calls alloc_anon() which instantly frees the allocated memory. alloc_anon_noexit() is usually used with cg_run_nowait() to run a process in the background that allocates memory. It makes sense for the background process to keep the memory allocated and not instantly free it (otherwise there is no point of running it in the background). Signed-off-by: Yosry Ahmed Acked-by: Roman Gushchin Acked-by: Shakeel Butt Acked-by: David Rientjes --- tools/testing/selftests/cgroup/test_memcontrol.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testi= ng/selftests/cgroup/test_memcontrol.c index 36ccf2322e21..f2ffb3a30194 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -211,13 +211,17 @@ static int alloc_pagecache_50M_noexit(const char *cgr= oup, void *arg) static int alloc_anon_noexit(const char *cgroup, void *arg) { int ppid =3D getppid(); + size_t size =3D (unsigned long)arg; + char *buf, *ptr; =20 - if (alloc_anon(cgroup, arg)) - return -1; + buf =3D malloc(size); + for (ptr =3D buf; ptr < buf + size; ptr +=3D PAGE_SIZE) + *ptr =3D 0; =20 while (getppid() =3D=3D ppid) sleep(1); =20 + free(buf); return 0; } =20 --=20 2.36.0.rc2.479.g8af0fa9b8e-goog From nobody Sun May 10 19:52:30 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D62DDC433F5 for ; Mon, 25 Apr 2022 19:01:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244823AbiDYTEU (ORCPT ); Mon, 25 Apr 2022 15:04:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240485AbiDYTEH (ORCPT ); Mon, 25 Apr 2022 15:04:07 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3689DEB80 for ; Mon, 25 Apr 2022 12:01:01 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id s2-20020a17090302c200b00158ea215fa2so10021299plk.3 for ; Mon, 25 Apr 2022 12:01:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=jNhmc3CPZ5vCVsb/llHs2xPQpQWQkNvnnQyZxpAVZvA=; b=Iq2S9Shz90p8NHXUzctrcXtVuymG0BHSMWdhf3g1aIk+jXNBHmBuLrOBkPz2g4i3wW I7e0S8dzX3XzREZmLBITiaxkxl465uGcVGpGess8no2z2MAudjeZ5DXw3Bxw73ZwGB7Q MRgm6I/dGRKXsHFripVwmfFPqfdFuclgGVwPjylXzWPgHqZNZPtB/1Jp2/oJ/W7F8y9S xGfsYuGC32TY9P82ZdD8i1tsMQF2qAPElU0/7vVYlioIFiFqD6K4zo+sVZzH9CF/FciX ais1AXji+6NbrGAuv46JxiDFpUDOZCEt5oQMPU1eKiljCUa03EJHxT2O52T/s7cWJYGy 3zbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=jNhmc3CPZ5vCVsb/llHs2xPQpQWQkNvnnQyZxpAVZvA=; b=AAgL/+yGjeDryGJwwrbkl1LNkRsxIAelec0BNrBjQB9vjQLC5tGU7xr5aRvWEHjxKV 1qNyGMRalhMuKiFBLb4JTtqgIO/p4vgtcwbta/WuSOiBlBpn7c5JjOv78QRQXRKv/H1Z D/L3CGM+2gIJZHcDzNw0ID1CLI/35l1Qb5palFUexiiCsWyMfsr5MGGxPhz/bWr9fEjt ci5bvfwOE1R3IY8mHq4Ge/axOLOtkRX7wMAZJcBMeAFjmOdqoUcQObfdEvd54LzsvF0t IIZJkKpidJ13aFxC+8WW+HbPrJeT+0EBOk1eHU6nKUvLo4BtvJ2tsQXCTLeLvrrvU12C rWSA== X-Gm-Message-State: AOAM531ezUn02EZCTjkDCiDbLPEa8WvHZPPa7ymw7wlFTCYAF77wHN+/ IhsVwAxZYsRxDtYadkj2LqaVYI0MqWA4GMD1 X-Google-Smtp-Source: ABdhPJx65fYn7YEk1pJGBjuWZR1gDqcxVKg0wWAyLHRkp/HuiCGSFGtSHC0vblXC2WzozjDBTDdEn4/m5NJEQ0y5 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a17:90a:9105:b0:1d2:9e98:7e1e with SMTP id k5-20020a17090a910500b001d29e987e1emr1604034pjo.0.1650913260401; Mon, 25 Apr 2022 12:01:00 -0700 (PDT) Date: Mon, 25 Apr 2022 19:00:40 +0000 In-Reply-To: <20220425190040.2475377-1-yosryahmed@google.com> Message-Id: <20220425190040.2475377-5-yosryahmed@google.com> Mime-Version: 1.0 References: <20220425190040.2475377-1-yosryahmed@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v5 4/4] selftests: cgroup: add a selftest for memory.reclaim From: Yosry Ahmed To: Johannes Weiner , Michal Hocko , Shakeel Butt , Andrew Morton , Roman Gushchin Cc: David Rientjes , Tejun Heo , Zefan Li , Jonathan Corbet , Shuah Khan , Yu Zhao , Dave Hansen , Wei Xu , Greg Thelen , Chen Wandun , Vaibhav Jain , "=?UTF-8?q?Michal=20Koutn=C3=BD?=" , Tim Chen , cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Yosry Ahmed Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a new test for memory.reclaim that verifies that the interface correctly reclaims memory as intended, from both anon and file pages. Signed-off-by: Yosry Ahmed Acked-by: Roman Gushchin Acked-by: David Rientjes --- .../selftests/cgroup/test_memcontrol.c | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testi= ng/selftests/cgroup/test_memcontrol.c index f2ffb3a30194..a9c691fb94c0 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -760,6 +760,111 @@ static int test_memcg_max(const char *root) return ret; } =20 +/* + * This test checks that memory.reclaim reclaims the given + * amount of memory (from both anon and file, if possible). + */ +static int test_memcg_reclaim(const char *root) +{ + int ret =3D KSFT_FAIL, fd, retries; + char *memcg; + long current, expected_usage, to_reclaim; + char buf[64]; + + memcg =3D cg_name(root, "memcg_test"); + if (!memcg) + goto cleanup; + + if (cg_create(memcg)) + goto cleanup; + + current =3D cg_read_long(memcg, "memory.current"); + if (current !=3D 0) + goto cleanup; + + fd =3D get_temp_fd(); + if (fd < 0) + goto cleanup; + + cg_run_nowait(memcg, alloc_pagecache_50M_noexit, (void *)(long)fd); + + /* + * If swap is enabled, try to reclaim from both anon and file, else try + * to reclaim from file only. + */ + if (is_swap_enabled()) { + cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(50)); + expected_usage =3D MB(100); + } else + expected_usage =3D MB(50); + + /* + * Wait until current usage reaches the expected usage (or we run out of + * retries). + */ + retries =3D 5; + while (!values_close(cg_read_long(memcg, "memory.current"), + expected_usage, 10)) { + if (retries--) { + sleep(1); + continue; + } else { + fprintf(stderr, + "failed to allocate %ld for memcg reclaim test\n", + expected_usage); + goto cleanup; + } + } + + /* + * Reclaim until current reaches 30M, this makes sure we hit both anon + * and file if swap is enabled. + */ + retries =3D 5; + while (true) { + int err; + + current =3D cg_read_long(memcg, "memory.current"); + to_reclaim =3D current - MB(30); + + /* + * We only keep looping if we get EAGAIN, which means we could + * not reclaim the full amount. + */ + if (to_reclaim <=3D 0) + goto cleanup; + + + snprintf(buf, sizeof(buf), "%ld", to_reclaim); + err =3D cg_write(memcg, "memory.reclaim", buf); + if (!err) { + /* + * If writing succeeds, then the written amount should have been + * fully reclaimed (and maybe more). + */ + current =3D cg_read_long(memcg, "memory.current"); + if (!values_close(current, MB(30), 3) && current > MB(30)) + goto cleanup; + break; + } + + /* The kernel could not reclaim the full amount, try again. */ + if (err =3D=3D -EAGAIN && retries--) + continue; + + /* We got an unexpected error or ran out of retries. */ + goto cleanup; + } + + ret =3D KSFT_PASS; +cleanup: + cg_destroy(memcg); + free(memcg); + close(fd); + + return ret; +} + static int alloc_anon_50M_check_swap(const char *cgroup, void *arg) { long mem_max =3D (long)arg; @@ -1263,6 +1368,7 @@ struct memcg_test { T(test_memcg_high), T(test_memcg_high_sync), T(test_memcg_max), + T(test_memcg_reclaim), T(test_memcg_oom_events), T(test_memcg_swap_max), T(test_memcg_sock), --=20 2.36.0.rc2.479.g8af0fa9b8e-goog