From patchwork Mon Jul 22 23:55:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Finkel X-Patchwork-Id: 13739231 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 A4F16C3DA7E for ; Mon, 22 Jul 2024 23:56:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 134196B0089; Mon, 22 Jul 2024 19:56:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E7C36B008A; Mon, 22 Jul 2024 19:56:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8F716B008C; Mon, 22 Jul 2024 19:56:06 -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 B8F466B0089 for ; Mon, 22 Jul 2024 19:56:06 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5E993C1A9B for ; Mon, 22 Jul 2024 23:56:06 +0000 (UTC) X-FDA: 82369049532.06.68F0DF0 Received: from m35-116.mailgun.net (m35-116.mailgun.net [69.72.35.116]) by imf18.hostedemail.com (Postfix) with ESMTP id 907801C0025 for ; Mon, 22 Jul 2024 23:56:04 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=relay.vimeo.com header.s=mailo header.b=mW2RcOUT; spf=pass (imf18.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"; dmarc=pass (policy=reject) header.from=vimeo.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721692542; 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=jsjLPBYavQVdLclCa8kvxT1KA4iDIPKvM8j0Os5u/Ok=; b=NDxmamtWYgi3t9eIqNyGWZGdqsnBuaTTEbPCqhY7ZfaMRCIrsFQhJfnYaqUg5EANCGwrD2 uy6b3kWgje4bYZjWFF2lT5/US18KV1k0zL95KBog0W8KfIh1nKGyctsRI1F/K4qnaHxwEH pAzJw6z85grvs20lVwKH/pqmM+Y/YAw= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=relay.vimeo.com header.s=mailo header.b=mW2RcOUT; spf=pass (imf18.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"; dmarc=pass (policy=reject) header.from=vimeo.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721692542; a=rsa-sha256; cv=none; b=MNFKhV5x4hoTeTu3a+RMVC9sjv20knEnq9LkvIgXZvyZ4S6C6wYp3gb2pGvsZcYedl/Ryi E8HS07Z5hgazO+DcgW8zwZ0raLQi8J/NaTZnbiCb/GFQt9QuluIDNiduZRQ5VXE+ufZq+Q Vcnv0uXDhetJqxad3Xnau4kv/lKIg6Y= DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=relay.vimeo.com; q=dns/txt; s=mailo; t=1721692563; x=1721699763; h=Content-Transfer-Encoding: MIME-Version: References: In-Reply-To: Message-Id: Date: Subject: Subject: Cc: To: To: From: From: Sender: Sender; bh=jsjLPBYavQVdLclCa8kvxT1KA4iDIPKvM8j0Os5u/Ok=; b=mW2RcOUTekC6+zlsQxf5PNpBammJ20zPMXM1CY50whaMgR2iHUaZ5WqCD3LXwju8vzZDT+botemCA7A24sqxle1JE/j4g9DMB4llB4KE22R0onwUmLgy0l8yIvxghYwFoAqsqoY4FHnaXzf1i1Jkn2x5rg3l2rjovP0MKokKGcM= 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 59065f94d682 with SMTP id 669ef193c143980835d4fbfd (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Mon, 22 Jul 2024 23:56:03 GMT Received: from nutau (gke-sre-us-east1-main-7f6ba6de-fx1q.c.vimeo-core.internal [10.56.27.207]) by smtp.vimeo.com (Postfix) with ESMTP id B860C64D5F; Mon, 22 Jul 2024 23:56:02 +0000 (UTC) Received: by nutau (Postfix, from userid 1001) id 88E78B4117E; Mon, 22 Jul 2024 19:56:02 -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, David Finkel Subject: [PATCH 2/2] mm, memcg: cg2 memory{.swap,}.peak write tests Date: Mon, 22 Jul 2024 19:55:54 -0400 Message-Id: <20240722235554.2911971-3-davidf@vimeo.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240722235554.2911971-1-davidf@vimeo.com> References: <20240722235554.2911971-1-davidf@vimeo.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 907801C0025 X-Stat-Signature: gako7bwn7gqofu5wewuaebmepxut9aqi X-HE-Tag: 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yI8FlAz12VIMjv+YoDC1fW00WlEnhfoaeRUY8BPwNcfgjL1DjyMhgJV5PEeJ6H5W+O9f/yQNMvGmmr7022AzH6Yk7n34ztueqrKTCrPDTln+/A0dGaRSGD6gCPBSpaDSqU0WzedubxwPP9+9/FbROidDoXx1kypI3DcqOymTNbzdPqKcEcK8U9EEgXfIOdy5mn5AM+h/N+Tp4fAthw1PvuF0OG4tlAcpjSPtBMIivggL6xOE7gTVh0iyRBSOO9W7s4oWHm/eVgC+kUFkYFY4EnZqlbVEtVcRjrzppNmwDiFfgqUTvtWvr4PC9wAPPHSKp9kvNWx8ayQLarmdVD0Y3owjNaGxj/jY3PpcZu0QigloMaRyH6s93bKVWFuE/ZEm4OZZ55JbZI3Ec6Nyq50TNDzp5gvp3xq6yAPpHFzec= X-Bogosity: Ham, tests=bogofilter, spamicity=0.001682, 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 --- .../selftests/cgroup/test_memcontrol.c | 227 +++++++++++++++++- 1 file changed, 219 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index 41ae8047b889..768f3e6c21af 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -161,12 +161,12 @@ 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; memcg = cg_name(root, "memcg_test"); @@ -180,15 +180,109 @@ 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. + */ + int peak_fd = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); + + if (peak_fd == -1) + goto cleanup; + + bool fd2_closed = false, fd3_closed = false, fd4_closed = false; + int peak_fd2 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); + + if (peak_fd2 == -1) + goto cleanup; + + int peak_fd3 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); + + if (peak_fd3 == -1) + goto cleanup; + + 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; + + int 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 +911,16 @@ 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; + + static const char reset_string[] = "reset\n"; if (!is_swap_enabled()) return KSFT_SKIP; @@ -840,6 +937,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 +998,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 +1060,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 +1506,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 +1514,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),