From patchwork Mon Jul 29 14:37:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Finkel X-Patchwork-Id: 13745823 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 D2B82C3DA70 for ; Mon, 29 Jul 2024 21:23:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E24EB6B0088; Mon, 29 Jul 2024 17:23:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DA41D6B008C; Mon, 29 Jul 2024 17:23:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ABDC46B008A; Mon, 29 Jul 2024 17:23:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7C3606B0085 for ; Mon, 29 Jul 2024 17:23:05 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0B5DAA4C19 for ; Mon, 29 Jul 2024 21:23:05 +0000 (UTC) X-FDA: 82394065530.13.5C825A1 Received: from m35-116.mailgun.net (m35-116.mailgun.net [69.72.35.116]) by imf21.hostedemail.com (Postfix) with ESMTP id 3B2C61C0015 for ; Mon, 29 Jul 2024 21:23:03 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=relay.vimeo.com header.s=mailo header.b="M/XK7BsX"; dmarc=pass (policy=reject) header.from=vimeo.com; spf=pass (imf21.hostedemail.com: domain of "bounce+ea57f2.9d2a1c-linux-mm=kvack.org@relay.vimeo.com" designates 69.72.35.116 as permitted sender) smtp.mailfrom="bounce+ea57f2.9d2a1c-linux-mm=kvack.org@relay.vimeo.com" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722288179; a=rsa-sha256; cv=none; b=l2ycuN+0qfgxhzQQXh/mPeUUWZT0L7evpFu0+2WQPpwRNUISwD4mtYIZIzBa+RWZrbuOb6 k4Q4haShWgbriF4ISJJ4iTN8NbFrYUOIXrIrWYFDc4w+GVlBaucqHB722C+f0XTVvXSnWP H2u61EWm/gRCzIBTKZ2bBYsxrIEju7U= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=relay.vimeo.com header.s=mailo header.b="M/XK7BsX"; dmarc=pass (policy=reject) header.from=vimeo.com; spf=pass (imf21.hostedemail.com: domain of "bounce+ea57f2.9d2a1c-linux-mm=kvack.org@relay.vimeo.com" designates 69.72.35.116 as permitted sender) smtp.mailfrom="bounce+ea57f2.9d2a1c-linux-mm=kvack.org@relay.vimeo.com" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722288179; h=from:from:sender: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:in-reply-to:references:references:dkim-signature; bh=H3UMxB4M7BrujHBPr+UgcfPfxgI2rlDhcD8iL3igSy4=; b=NVTBXs5vMta+QQXLq3F4T+A/AihVVD/RnlxZ7BGIEhONTo0Kj6p9n/d+ol7k/PtMk66eJR 0zHqW0ZZGxmU5VeDZkIdqChcGCxG/Wwb2USiI098238zXnodRVBtLJM9iJinWLOut0lOxv tPJxkQ4wXB1QQknV3W04crIK5hrQ7KM= DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=relay.vimeo.com; q=dns/txt; s=mailo; t=1722288182; x=1722295382; h=Content-Transfer-Encoding: MIME-Version: References: In-Reply-To: Message-Id: Date: Subject: Subject: Cc: To: To: From: From: Sender: Sender; bh=H3UMxB4M7BrujHBPr+UgcfPfxgI2rlDhcD8iL3igSy4=; b=M/XK7BsXSHOLe84nAHAP6i7AQUWOWpeNieQQpAzlDNlm1zDrk3+8SRY3v7RESQbawpx0wg+D+ZpzBSF6McFuOLRdlvlCbgS8Vra0Bz1A2wz3FqttSqkjGR9z0ovXZiwZmVgRFMVf2QDwPm2kiiYuDzZ7yW2zAEb6ciDNxA2xLJ4= X-Mailgun-Sending-Ip: 69.72.35.116 X-Mailgun-Sid: WyI5NTRmYiIsImxpbnV4LW1tQGt2YWNrLm9yZyIsIjlkMmExYyJd Received: from smtp.vimeo.com (215.71.185.35.bc.googleusercontent.com [35.185.71.215]) by 5a80eff7c455 with SMTP id 66a808364527901af3858a25 (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Mon, 29 Jul 2024 21:23:02 GMT Received: from nutau (gke-sre-us-east1-main-7f6ba6de-loec.c.vimeo-core.internal [10.56.27.213]) by smtp.vimeo.com (Postfix) with ESMTP id 17B7C65CBB; Mon, 29 Jul 2024 21:23:02 +0000 (UTC) Received: by nutau (Postfix, from userid 1001) id 0D850B40AC9; Mon, 29 Jul 2024 10:38:11 -0400 (EDT) From: David Finkel To: Muchun Song , Tejun Heo , Roman Gushchin , Andrew Morton Cc: core-services@vimeo.com, Jonathan Corbet , Michal Hocko , Shakeel Butt , Shuah Khan , Johannes Weiner , Zefan Li , cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Michal_Koutn=C3=BD?= , David Finkel Subject: [PATCH v6 2/2] mm, memcg: cg2 memory{.swap,}.peak write tests Date: Mon, 29 Jul 2024 10:37:43 -0400 Message-Id: <20240729143743.34236-3-davidf@vimeo.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240729143743.34236-1-davidf@vimeo.com> References: <20240729143743.34236-1-davidf@vimeo.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 3B2C61C0015 X-Rspamd-Server: rspam01 X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gCpggt0HHbbomYDjyoY6Il8cBSzxTxYMS8JAn7Uo3337xU829FEkRResaXYTvBP/8NJfTJFj2tJzlkWA/0xXiaAqH0qFE4u9xyK5sNw8b+x4gtXZ5O+zAjMrS9vWoHUtdfy3DvQSBUPrhWeolBA1ByIWoGII7SVkeaCyTQ4HtQu3Nbdv0HyZvAoApuSbn6LboVnAKetoCUP8a9zFsDr74L50pIGvUfn42dcBjj4KyP5u5P3xkDED7ejYeNf+7rZsDZlnQo5uogYLIrEuYcD80pkdL18SDp3psVd46im3bBv2aVP+bOZlkyw1XTIpeAgEdD7BkhcGaTunNkADYVSx2HOEyZQzf1lftIknakUetB5JvISUTJd/ulJoOKvTbwETrbcgsznp1tfmHt2XiiBOkv6dZCMrOLcKsL6fmE6OekKoALNmCTCT83upeurrr5H8hRoLj2BE/wC7qEc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.001874, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Extend two existing tests to cover extracting memory usage through the newly mutable memory.peak and memory.swap.peak handlers. In particular, make sure to exercise adding and removing watchers with overlapping lifetimes so the less-trivial logic gets tested. Signed-off-by: David Finkel --- tools/testing/selftests/cgroup/cgroup_util.c | 22 ++ tools/testing/selftests/cgroup/cgroup_util.h | 2 + .../selftests/cgroup/test_memcontrol.c | 229 +++++++++++++++++- 3 files changed, 245 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/cgroup_util.c index 432db923bced0..1e2d46636a0ca 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.c +++ b/tools/testing/selftests/cgroup/cgroup_util.c @@ -141,6 +141,16 @@ long cg_read_long(const char *cgroup, const char *control) return atol(buf); } +long cg_read_long_fd(int fd) +{ + char buf[128]; + + if (pread(fd, buf, sizeof(buf), 0) <= 0) + return -1; + + return atol(buf); +} + long cg_read_key_long(const char *cgroup, const char *control, const char *key) { char buf[PAGE_SIZE]; @@ -183,6 +193,18 @@ int cg_write(const char *cgroup, const char *control, char *buf) return ret == len ? 0 : ret; } +/* + * Returns fd on success, or -1 on failure. + * (fd should be closed with close() as usual) + */ +int cg_open(const char *cgroup, const char *control, int flags) +{ + char path[PATH_MAX]; + + snprintf(path, sizeof(path), "%s/%s", cgroup, control); + return open(path, flags); +} + int cg_write_numeric(const char *cgroup, const char *control, long value) { char buf[64]; diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/selftests/cgroup/cgroup_util.h index e8d04ac9e3d23..19b131ee77072 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.h +++ b/tools/testing/selftests/cgroup/cgroup_util.h @@ -34,9 +34,11 @@ extern int cg_read_strcmp(const char *cgroup, const char *control, extern int cg_read_strstr(const char *cgroup, const char *control, const char *needle); extern long cg_read_long(const char *cgroup, const char *control); +extern long cg_read_long_fd(int fd); long cg_read_key_long(const char *cgroup, const char *control, const char *key); extern long cg_read_lc(const char *cgroup, const char *control); extern int cg_write(const char *cgroup, const char *control, char *buf); +extern int cg_open(const char *cgroup, const char *control, int flags); int cg_write_numeric(const char *cgroup, const char *control, long value); extern int cg_run(const char *cgroup, int (*fn)(const char *cgroup, void *arg), diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index 41ae8047b8895..f54c1f75b6da7 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -161,13 +161,15 @@ static int alloc_pagecache_50M_check(const char *cgroup, void *arg) /* * This test create a memory cgroup, allocates * some anonymous memory and some pagecache - * and check memory.current and some memory.stat values. + * and checks memory.current, memory.peak, and some memory.stat values. */ -static int test_memcg_current(const char *root) +static int test_memcg_current_peak(const char *root) { int ret = KSFT_FAIL; - long current; + long current, peak, peak_reset; char *memcg; + bool fd2_closed = false, fd3_closed = false, fd4_closed = false; + int peak_fd = -1, peak_fd2 = -1, peak_fd3 = -1, peak_fd4 = -1; memcg = cg_name(root, "memcg_test"); if (!memcg) @@ -180,15 +182,108 @@ static int test_memcg_current(const char *root) if (current != 0) goto cleanup; + peak = cg_read_long(memcg, "memory.peak"); + if (peak != 0) + goto cleanup; + if (cg_run(memcg, alloc_anon_50M_check, NULL)) goto cleanup; + peak = cg_read_long(memcg, "memory.peak"); + if (peak < MB(50)) + goto cleanup; + + /* + * We'll open a few FDs for the same memory.peak file to exercise the free-path + * We need at least three to be closed in a different order than writes occurred to test + * the linked-list handling. + */ + peak_fd = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); + + if (peak_fd == -1) + goto cleanup; + + peak_fd2 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); + + if (peak_fd2 == -1) + goto cleanup; + + peak_fd3 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); + + if (peak_fd3 == -1) + goto cleanup; + + /* any non-empty string resets, but make it clear */ + static const char reset_string[] = "reset\n"; + + peak_reset = write(peak_fd, reset_string, sizeof(reset_string)); + if (peak_reset != sizeof(reset_string)) + goto cleanup; + + peak_reset = write(peak_fd2, reset_string, sizeof(reset_string)); + if (peak_reset != sizeof(reset_string)) + goto cleanup; + + peak_reset = write(peak_fd3, reset_string, sizeof(reset_string)); + if (peak_reset != sizeof(reset_string)) + goto cleanup; + + /* Make sure a completely independent read isn't affected by our FD-local reset above*/ + peak = cg_read_long(memcg, "memory.peak"); + if (peak < MB(50)) + goto cleanup; + + fd2_closed = true; + if (close(peak_fd2)) + goto cleanup; + + peak_fd4 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); + + if (peak_fd4 == -1) + goto cleanup; + + peak_reset = write(peak_fd4, reset_string, sizeof(reset_string)); + if (peak_reset != sizeof(reset_string)) + goto cleanup; + + peak = cg_read_long_fd(peak_fd); + if (peak > MB(30) || peak < 0) + goto cleanup; + if (cg_run(memcg, alloc_pagecache_50M_check, NULL)) goto cleanup; + peak = cg_read_long(memcg, "memory.peak"); + if (peak < MB(50)) + goto cleanup; + + /* Make sure everything is back to normal */ + peak = cg_read_long_fd(peak_fd); + if (peak < MB(50)) + goto cleanup; + + peak = cg_read_long_fd(peak_fd4); + if (peak < MB(50)) + goto cleanup; + + fd3_closed = true; + if (close(peak_fd3)) + goto cleanup; + + fd4_closed = true; + if (close(peak_fd4)) + goto cleanup; + ret = KSFT_PASS; cleanup: + close(peak_fd); + if (!fd2_closed) + close(peak_fd2); + if (!fd3_closed) + close(peak_fd3); + if (!fd4_closed) + close(peak_fd4); cg_destroy(memcg); free(memcg); @@ -817,13 +912,17 @@ static int alloc_anon_50M_check_swap(const char *cgroup, void *arg) /* * This test checks that memory.swap.max limits the amount of - * anonymous memory which can be swapped out. + * anonymous memory which can be swapped out. Additionally, it verifies that + * memory.swap.peak reflects the high watermark and can be reset. */ -static int test_memcg_swap_max(const char *root) +static int test_memcg_swap_max_peak(const char *root) { int ret = KSFT_FAIL; char *memcg; - long max; + long max, peak; + + /* any non-empty string resets */ + static const char reset_string[] = "foobarbaz"; if (!is_swap_enabled()) return KSFT_SKIP; @@ -840,6 +939,45 @@ static int test_memcg_swap_max(const char *root) goto cleanup; } + int swap_peak_fd = cg_open(memcg, "memory.swap.peak", + O_RDWR | O_APPEND | O_CLOEXEC); + + if (swap_peak_fd == -1) + goto cleanup; + + int mem_peak_fd = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); + + if (mem_peak_fd == -1) + goto cleanup; + + if (cg_read_long(memcg, "memory.swap.peak")) + goto cleanup; + + if (cg_read_long_fd(swap_peak_fd)) + goto cleanup; + + /* switch the swap and mem fds into local-peak tracking mode*/ + int peak_reset = write(swap_peak_fd, reset_string, sizeof(reset_string)); + + if (peak_reset != sizeof(reset_string)) + goto cleanup; + + if (cg_read_long_fd(swap_peak_fd)) + goto cleanup; + + if (cg_read_long(memcg, "memory.peak")) + goto cleanup; + + if (cg_read_long_fd(mem_peak_fd)) + goto cleanup; + + peak_reset = write(mem_peak_fd, reset_string, sizeof(reset_string)); + if (peak_reset != sizeof(reset_string)) + goto cleanup; + + if (cg_read_long_fd(mem_peak_fd)) + goto cleanup; + if (cg_read_strcmp(memcg, "memory.max", "max\n")) goto cleanup; @@ -862,6 +1000,61 @@ static int test_memcg_swap_max(const char *root) if (cg_read_key_long(memcg, "memory.events", "oom_kill ") != 1) goto cleanup; + peak = cg_read_long(memcg, "memory.peak"); + if (peak < MB(29)) + goto cleanup; + + peak = cg_read_long(memcg, "memory.swap.peak"); + if (peak < MB(29)) + goto cleanup; + + peak = cg_read_long_fd(mem_peak_fd); + if (peak < MB(29)) + goto cleanup; + + peak = cg_read_long_fd(swap_peak_fd); + if (peak < MB(29)) + goto cleanup; + + /* + * open, reset and close the peak swap on another FD to make sure + * multiple extant fds don't corrupt the linked-list + */ + peak_reset = cg_write(memcg, "memory.swap.peak", (char *)reset_string); + if (peak_reset) + goto cleanup; + + peak_reset = cg_write(memcg, "memory.peak", (char *)reset_string); + if (peak_reset) + goto cleanup; + + /* actually reset on the fds */ + peak_reset = write(swap_peak_fd, reset_string, sizeof(reset_string)); + if (peak_reset != sizeof(reset_string)) + goto cleanup; + + peak_reset = write(mem_peak_fd, reset_string, sizeof(reset_string)); + if (peak_reset != sizeof(reset_string)) + goto cleanup; + + peak = cg_read_long_fd(swap_peak_fd); + if (peak > MB(10)) + goto cleanup; + + /* + * The cgroup is now empty, but there may be a page or two associated + * with the open FD accounted to it. + */ + peak = cg_read_long_fd(mem_peak_fd); + if (peak > MB(1)) + goto cleanup; + + if (cg_read_long(memcg, "memory.peak") < MB(29)) + goto cleanup; + + if (cg_read_long(memcg, "memory.swap.peak") < MB(29)) + goto cleanup; + if (cg_run(memcg, alloc_anon_50M_check_swap, (void *)MB(30))) goto cleanup; @@ -869,9 +1062,29 @@ static int test_memcg_swap_max(const char *root) if (max <= 0) goto cleanup; + peak = cg_read_long(memcg, "memory.peak"); + if (peak < MB(29)) + goto cleanup; + + peak = cg_read_long(memcg, "memory.swap.peak"); + if (peak < MB(29)) + goto cleanup; + + peak = cg_read_long_fd(mem_peak_fd); + if (peak < MB(29)) + goto cleanup; + + peak = cg_read_long_fd(swap_peak_fd); + if (peak < MB(19)) + goto cleanup; + ret = KSFT_PASS; cleanup: + if (close(mem_peak_fd)) + ret = KSFT_FAIL; + if (close(swap_peak_fd)) + ret = KSFT_FAIL; cg_destroy(memcg); free(memcg); @@ -1295,7 +1508,7 @@ struct memcg_test { const char *name; } tests[] = { T(test_memcg_subtree_control), - T(test_memcg_current), + T(test_memcg_current_peak), T(test_memcg_min), T(test_memcg_low), T(test_memcg_high), @@ -1303,7 +1516,7 @@ struct memcg_test { T(test_memcg_max), T(test_memcg_reclaim), T(test_memcg_oom_events), - T(test_memcg_swap_max), + T(test_memcg_swap_max_peak), T(test_memcg_sock), T(test_memcg_oom_group_leaf_events), T(test_memcg_oom_group_parent_events),