From patchwork Wed Nov 23 09:21:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13053288 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 2B47BC4332F for ; Wed, 23 Nov 2022 09:21:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AEFB48E0002; Wed, 23 Nov 2022 04:21:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A78E48E0001; Wed, 23 Nov 2022 04:21:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F2858E0002; Wed, 23 Nov 2022 04:21:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 80F728E0001 for ; Wed, 23 Nov 2022 04:21:45 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5D5AF411F4 for ; Wed, 23 Nov 2022 09:21:45 +0000 (UTC) X-FDA: 80164164570.15.30AA5C9 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf09.hostedemail.com (Postfix) with ESMTP id 0F5DA14000B for ; Wed, 23 Nov 2022 09:21:44 +0000 (UTC) Received: by mail-pj1-f73.google.com with SMTP id c10-20020a17090aa60a00b00212e91df6acso8659518pjq.5 for ; Wed, 23 Nov 2022 01:21:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zZ8JuQS8X9t3i4zrgE/S0Ri9wo/GkYIgsNIx6OEFr5o=; b=DEp8BSTd9VIwtnULP6VzWamTubDDP1gDZgCtUL3t/6x4ArdhrAAY+23qy7pz5jAyW9 hLlIbnT90c1Tcw3JhR6bcFVLBaJXvv/TICLht+RD563Od8KYY0M9H7AwOaE8hkyi+4P4 lAEpAQvnKj8LcQZvAPh5ONRxCteCpkOxZFqu2HI97mVrs24uh8gbeIWiwFXqbA01HNIQ oqzk4l0p28W4KMTCuTNQqh+eKppRX/STqk/GYhGQ3ygXEegrR4ifdKGyGqXuYdDqtcK/ YrKqTZIheFyOuQXeSjmaXlXOKUutxGa8Ub6CnWuSRIBjbAKKxVKbtELfuCSM/AFejFol ekRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zZ8JuQS8X9t3i4zrgE/S0Ri9wo/GkYIgsNIx6OEFr5o=; b=WnC6lPBfCtU8BFsT3IsZWaUgpDYxf87LaHeg+nVjmn43lWTi7a4PseOh9ez+MpUQfC 4QH4hQjmaNdaeQhTuVZnAXObxHHG68BgyQM+qstzD1cKXhRHepEsC1FrIiAw+QH4ofeD XZjkiJmAS2aldI/j4PyZ0AG/D1TDdOfs4m/hIDAEg0ppQoyYwWUgO32LlKl+akdn8UpQ D8Mq4U16lT6S0b869ejxsHh5cbBnwbwR1p7Hy4K44+Bf3ClxfAWQkyFykmvbMhfYcbvC cCC5jfS5R9PzBb2CI/AJ4yeLPUmrd6TiuXTZAEtwKMKB613TARoCALTDyWi6gT1rY5aW kxTA== X-Gm-Message-State: ANoB5pmmUP8kPOb8Jl8QTh5AipIjVSI8zeMIdHgc9FAFp5ce3T27BkoZ UV9FPbDBx8UCC9gl0Ef2BrtfgMhrt3a9m9yS X-Google-Smtp-Source: AA0mqf7lNnDk3AMn4WDks5KSg+GBb6+n4WUj4qBI2dF5jmITLTNcCXXmiBK6oEulkWnC0jyWRsCxudd0bBgcirOa X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a17:902:f2c5:b0:189:1cc3:802a with SMTP id h5-20020a170902f2c500b001891cc3802amr9596140plc.56.1669195304144; Wed, 23 Nov 2022 01:21:44 -0800 (PST) Date: Wed, 23 Nov 2022 09:21:31 +0000 In-Reply-To: <20221123092132.2521764-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20221123092132.2521764-1-yosryahmed@google.com> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog Message-ID: <20221123092132.2521764-3-yosryahmed@google.com> Subject: [PATCH v2 2/3] selftests: cgroup: refactor proactive reclaim code to reclaim_until() From: Yosry Ahmed To: Shakeel Butt , Roman Gushchin , Johannes Weiner , Michal Hocko , Yu Zhao , Muchun Song Cc: "Matthew Wilcox (Oracle)" , Vasily Averin , Vlastimil Babka , Chris Down , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Yosry Ahmed ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DEp8BSTd; spf=pass (imf09.hostedemail.com: domain of 3KOZ9YwoKCA8D376Dpw1tsv33v0t.r310x29C-11zAprz.36v@flex--yosryahmed.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3KOZ9YwoKCA8D376Dpw1tsv33v0t.r310x29C-11zAprz.36v@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669195305; a=rsa-sha256; cv=none; b=ygv7nPM7RjBg5RDRMa2UGW/eIYKz/gPtu5kBMDbTB0w625bsCXW9QKgIsTU6Sv3uxME+Di T1oJj5nu+cffG3Th1XxbXUwZMBLvvf8yVE1dmTH68WBh9BGT4XIGgV1hdmH1grlO6mG91c cg0fHK4HG5ZDSjWuy96FCbyE3LbA4dw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669195305; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zZ8JuQS8X9t3i4zrgE/S0Ri9wo/GkYIgsNIx6OEFr5o=; b=6iKqdQN1bBQhBR+vNW2HsxDM2HkefUbKmJM+wVx0/SHprIbyFJ+QcrNSIs/OHbgMSs+UEJ nll+EwvHEZQiD+PSfIc7gIj3B0JpWSZASRcjk6Flk1QomN9wvR3cfOeIKT2NvGLQ5W5t40 sdPlK5INZvM5696+S6VBZx9Z83MvGA8= X-Stat-Signature: e4dejdnupoq3uq8fiax3fbgbhrnbgmyk X-Rspamd-Server: rspam08 X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DEp8BSTd; spf=pass (imf09.hostedemail.com: domain of 3KOZ9YwoKCA8D376Dpw1tsv33v0t.r310x29C-11zAprz.36v@flex--yosryahmed.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3KOZ9YwoKCA8D376Dpw1tsv33v0t.r310x29C-11zAprz.36v@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Queue-Id: 0F5DA14000B X-HE-Tag: 1669195304-605516 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: Refactor the code that drives writing to memory.reclaim (retrying, error handling, etc) from test_memcg_reclaim() to a helper called reclaim_until(), which proactively reclaims from a memcg until its usage reaches a certain value. This will be used in a following patch in another test. Signed-off-by: Yosry Ahmed Reviewed-by: Roman Gushchin Signed-off-by: Yosry Ahmed --- .../selftests/cgroup/test_memcontrol.c | 85 +++++++++++-------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index 8833359556f3..d4182e94945e 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -645,6 +645,53 @@ static int test_memcg_max(const char *root) return ret; } +/* Reclaim from @memcg until usage reaches @goal_usage */ +static bool reclaim_until(const char *memcg, long goal_usage) +{ + char buf[64]; + int retries = 5; + int err; + long current, to_reclaim; + + /* Nothing to do here */ + if (cg_read_long(memcg, "memory.current") <= goal_usage) + return true; + + while (true) { + current = cg_read_long(memcg, "memory.current"); + to_reclaim = current - goal_usage; + + /* + * We only keep looping if we get -EAGAIN, which means we could + * not reclaim the full amount. This means we got -EAGAIN when + * we actually reclaimed the requested amount, so fail. + */ + if (to_reclaim <= 0) + break; + + snprintf(buf, sizeof(buf), "%ld", to_reclaim); + err = cg_write(memcg, "memory.reclaim", buf); + if (!err) { + /* + * If writing succeeds, then the written amount should have been + * fully reclaimed (and maybe more). + */ + current = cg_read_long(memcg, "memory.current"); + if (!values_close(current, goal_usage, 3) && current > goal_usage) + break; + return true; + } + + /* The kernel could not reclaim the full amount, try again. */ + if (err == -EAGAIN && retries--) + continue; + + /* We got an unexpected error or ran out of retries. */ + break; + } + return false; +} + /* * This test checks that memory.reclaim reclaims the given * amount of memory (from both anon and file, if possible). @@ -653,8 +700,7 @@ static int test_memcg_reclaim(const char *root) { int ret = KSFT_FAIL, fd, retries; char *memcg; - long current, expected_usage, to_reclaim; - char buf[64]; + long current, expected_usage; memcg = cg_name(root, "memcg_test"); if (!memcg) @@ -705,41 +751,8 @@ static int test_memcg_reclaim(const char *root) * Reclaim until current reaches 30M, this makes sure we hit both anon * and file if swap is enabled. */ - retries = 5; - while (true) { - int err; - - current = cg_read_long(memcg, "memory.current"); - to_reclaim = current - MB(30); - - /* - * We only keep looping if we get EAGAIN, which means we could - * not reclaim the full amount. - */ - if (to_reclaim <= 0) - goto cleanup; - - - snprintf(buf, sizeof(buf), "%ld", to_reclaim); - err = cg_write(memcg, "memory.reclaim", buf); - if (!err) { - /* - * If writing succeeds, then the written amount should have been - * fully reclaimed (and maybe more). - */ - current = 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 == -EAGAIN && retries--) - continue; - - /* We got an unexpected error or ran out of retries. */ + if (!reclaim_until(memcg, MB(30))) goto cleanup; - } ret = KSFT_PASS; cleanup: