From nobody Thu Sep 18 21:55:08 2025 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 45137C4332F for ; Fri, 2 Dec 2022 03:15:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231955AbiLBDPm (ORCPT ); Thu, 1 Dec 2022 22:15:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231908AbiLBDPf (ORCPT ); Thu, 1 Dec 2022 22:15:35 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7183AD78D4 for ; Thu, 1 Dec 2022 19:15:34 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id p6-20020a170902e74600b001896ba6837bso4674797plf.17 for ; Thu, 01 Dec 2022 19:15:34 -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=DuRs+eCOo373fWFIamKdpS1DYx9E7KO6vBATpERNpN8=; b=P8OpmCy+hsdPsPghjVdy/8p6gbBKyWTBeA78u/eWGmraKF4XDkTC9BUAU4xols3RBJ Eb3T4ls0TY54/uxuYu7J+7atf4/xsoYt0KtUeRT9Zb5hY23o7MjxGkNxTpE6HlEG6JLX U4PTYctA7PHrrQ/iwUqeB94jmIuWs5SyK7sWXKJ1Nk2bdcheYAGV4dLM23SFnBy69R0L vBw+xnHv9O+RKwOcXilgLaN2TxlnG+nleMjWKyBjPB5guj+1LDHVlpKt8044WVFp6kV+ prfW9gEk3LyWoDGd81vPJOKNBNtQiWQcZaS9NglCCm5pty1GVtmv0As3GfjkM/d/SVij WH3g== 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=DuRs+eCOo373fWFIamKdpS1DYx9E7KO6vBATpERNpN8=; b=AILXxk2TH90CUHZHGTIBl3UP4R8SvzLwhNdI+Qw5zGdStX8u36rJyYUn6QgDEKO46h dRIcT+Yy+00TMHpyoSMrzxRF0u0uauv2wPIpSDMn//gEkwvrKjc4QfuM8FueMTTjixld 7LXUX8hV83MgHJ1XDgoWEaqD2sJUOs55dIspjOVRK0yvKVN6fu+EqPsiD6wB4fJwxQ8F C/0DYn8Mr7MuaFTVQ8lWDrYEz2iP9WZuN0hU0nhWiCa3yWvM8SOitzCoHJ7ktksOZQsg ZGYWZOC5ss+EFbda5jW4bT2vKJKyi2eI3OqeqKuNbiSuJzTocRjBpltr6M01XHliIW0Z jhmw== X-Gm-Message-State: ANoB5pnZHPuOyrnON8x/GP6ZCxyzbeNrj4E33w5INdS+5id8Xq+yI+Jl YrIc+/Z8mG9TWYF1IywkSSAXTKDkdlm5x06U X-Google-Smtp-Source: AA0mqf6TTfBSxhIz81VhCaQGBqVtXZxPisNhr7Boyb7mKndn+6VVW50DIORS98OFuwLJLcf1hGpLLhUt4jIB/zNT X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a05:6a00:4396:b0:575:1493:88d0 with SMTP id bt22-20020a056a00439600b00575149388d0mr25914503pfb.70.1669950933870; Thu, 01 Dec 2022 19:15:33 -0800 (PST) Date: Fri, 2 Dec 2022 03:15:11 +0000 In-Reply-To: <20221202031512.1365483-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20221202031512.1365483-1-yosryahmed@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202031512.1365483-3-yosryahmed@google.com> Subject: [PATCH v3 2/3] selftests: cgroup: refactor proactive reclaim code to reclaim_until() From: Yosry Ahmed To: Andrew Morton , Shakeel Butt , Roman Gushchin , Johannes Weiner , Michal Hocko , Yu Zhao , Muchun Song , Tejun Heo Cc: "Matthew Wilcox (Oracle)" , Vasily Averin , Vlastimil Babka , Chris Down , David Rientjes , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.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" 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. While we are at it, refactor and simplify the reclaim loop. This will be used in a following patch in another test. Signed-off-by: Yosry Ahmed Suggested-by: Roman Gushchin Reviewed-by: Roman Gushchin --- .../selftests/cgroup/test_memcontrol.c | 80 ++++++++++--------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testi= ng/selftests/cgroup/test_memcontrol.c index 8833359556f3..a8f4700353a4 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -645,6 +645,48 @@ static int test_memcg_max(const char *root) return ret; } =20 +/* + * Reclaim from @memcg until usage reaches @goal by writing to + * memory.reclaim. + * + * This function will return false if the usage is already below the + * goal. + * + * This function assumes that writing to memory.reclaim is the only + * source of change in memory.current (no concurrent allocations or + * reclaim). + * + * This function makes sure memory.reclaim is sane. It will return + * false if memory.reclaim's error codes do not make sense, even if + * the usage goal was satisfied. + */ +static bool reclaim_until(const char *memcg, long goal) +{ + char buf[64]; + int retries, err; + long current, to_reclaim; + bool reclaimed =3D false; + + for (retries =3D 5; retries > 0; retries--) { + current =3D cg_read_long(memcg, "memory.current"); + + if (current < goal || values_close(current, goal, 3)) + break; + /* Did memory.reclaim return 0 incorrectly? */ + else if (reclaimed) + return false; + + to_reclaim =3D current - goal; + snprintf(buf, sizeof(buf), "%ld", to_reclaim); + err =3D cg_write(memcg, "memory.reclaim", buf); + if (!err) + reclaimed =3D true; + else if (err !=3D -EAGAIN) + return false; + } + return reclaimed; +} + /* * This test checks that memory.reclaim reclaims the given * amount of memory (from both anon and file, if possible). @@ -653,8 +695,7 @@ 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]; + long current, expected_usage; =20 memcg =3D cg_name(root, "memcg_test"); if (!memcg) @@ -705,41 +746,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 =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. */ + if (!reclaim_until(memcg, MB(30))) goto cleanup; - } =20 ret =3D KSFT_PASS; cleanup: --=20 2.39.0.rc0.267.gcb52ba06e7-goog