Message ID | 20221123092132.2521764-3-yosryahmed@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2681588wrr; Wed, 23 Nov 2022 01:22:58 -0800 (PST) X-Google-Smtp-Source: AA0mqf6dEEx6mwAzJaQv4amr2gTxb7sIPAcQGcfc2mmRtxG/9eRnp3YevZmO8lowsukh+k38glhh X-Received: by 2002:a17:90a:8a13:b0:20a:e24d:5e32 with SMTP id w19-20020a17090a8a1300b0020ae24d5e32mr35686528pjn.138.1669195377886; Wed, 23 Nov 2022 01:22:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669195377; cv=none; d=google.com; s=arc-20160816; b=OCBCjNuxN1cukApxDg56lufTMXQDvFeAkaBM/kFsTalvfl+5DR61u5xokB+0dK9noi k+ZWVEdWDfynfk6FgOPv9KTtAwOtDokYihw/9NllRPSqsuRqmDuDjF6gx36Sug6H7ByJ h2j6xq7/BsUbERSG04D+8W8zRukqV4dNShwWhkBuKPKerJWgpeoTkfE+S591/bLIk1sl Ap77t908hW1vS1QBkTvXDlaMu7fVM3iruaIg+WnaeJTGhdVoTzlO6GrD9iPSH62d+W9b xcXresyrc7tg5lKcFTvY+RbHqGqmhszH1si5rvtkQakH9MReK7cjsS0RzeoMTBtj1fh/ 1oEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=zZ8JuQS8X9t3i4zrgE/S0Ri9wo/GkYIgsNIx6OEFr5o=; b=lAWcTV2jGZOwTx1c5WHTlQiRgcA4iSb46NJKxECTGeOwg2klD6xXclnN78Hdjkp6P1 zT5dwmwaZWo4NTPlM/8UPUEkXv6ml2/zxLFAGAX5s0xtblbJGaaKZei9vfXkk8Sgp8y5 dDhi3pPIjC/ZUnx2g5CVOV0DWF9117ZenYg81bYAN4IVe6GXkRQK5Ah05tULAkADJz8Z KzxavzxLN95/6Wix4u6361TtDtF+KjiWlCtgzrZlU0GO2Os8zKNbPIGb5xm/F2k99Fk4 RNMej9sMijUd64cmeuHs68LHX+hig4YxQc8daenEnaHEJCW2gAf9cQ/vhxT0hF0d3wa5 2r9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=DEp8BSTd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v24-20020a17090331d800b001893e9d712dsi1564422ple.557.2022.11.23.01.22.44; Wed, 23 Nov 2022 01:22:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=DEp8BSTd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237552AbiKWJWb (ORCPT <rfc822;cjcooper78@gmail.com> + 99 others); Wed, 23 Nov 2022 04:22:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237621AbiKWJWB (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 23 Nov 2022 04:22:01 -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 8B48D10613D for <linux-kernel@vger.kernel.org>; Wed, 23 Nov 2022 01:21:44 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id p18-20020a170902a41200b00188e87d0f04so13484083plq.14 for <linux-kernel@vger.kernel.org>; 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=qtg3ts27UoFDaQRZp9Lko7qLMiBnJqyq+78sgNbojvgw7LFQcvpjvYue65XnkSq1Kc otdGVOlGeFSORxNRIV+QScKCX899KXDzb/jtyl/xhSYtMqaS17hs/ncKFvsst89ABy0N 1kSXTPgJEZ2fns9IpXOf202F1lQv1S+R481TdHXwZNPnQXbmlu07werKvz/GFPZotsvM bcqcEw7N7e39SbIHARR0InHCEI7myyq0Ukn5rxS1IEtuNsUb9En4fYC1rgBJU5RdpVZe O7kylnKR2+3vkjrNLttq8xmr5KGtidaHSfHiEPolK2r0yABwuLdqjvNKZ8kTp8e5jAFE saVg== X-Gm-Message-State: ANoB5pmA7t0Lg/M6A4b94tPk0CusBT7cTd83lYmo0iPkcdfEiloZpPVj OoRo4VRiue2zfQImo8Qfze8MuKFC6NUe9SG6 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 <yosryahmed@google.com> To: Shakeel Butt <shakeelb@google.com>, Roman Gushchin <roman.gushchin@linux.dev>, Johannes Weiner <hannes@cmpxchg.org>, Michal Hocko <mhocko@suse.com>, Yu Zhao <yuzhao@google.com>, Muchun Song <songmuchun@bytedance.com> Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>, Vasily Averin <vasily.averin@linux.dev>, Vlastimil Babka <vbabka@suse.cz>, Chris Down <chris@chrisdown.name>, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Yosry Ahmed <yosryahmed@google.com> Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750278212781495175?= X-GMAIL-MSGID: =?utf-8?q?1750278212781495175?= |
Series |
mm: memcg: fix protection of reclaim target memcg
|
|
Commit Message
Yosry Ahmed
Nov. 23, 2022, 9:21 a.m. UTC
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 <yosryahmed@google.com>
---
.../selftests/cgroup/test_memcontrol.c | 85 +++++++++++--------
1 file changed, 49 insertions(+), 36 deletions(-)
Comments
On Wed, Nov 23, 2022 at 09:21:31AM +0000, Yosry Ahmed wrote: > 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 <yosryahmed@google.com> > --- > .../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; The code below looks correct, but can be simplified a bit. And btw thank you for adding a test! Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev> (idk if you want invest your time in further simplication of this code, it was this way before this patch, so up to you). > } > > +/* 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; There are 3 places in this function where memory.current is read and compared to goal_usage. I believe only one can be left. > + 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; if (err != -EAGAIN || retries--) break; Thanks!
On Wed, Nov 23, 2022 at 5:03 PM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > On Wed, Nov 23, 2022 at 09:21:31AM +0000, Yosry Ahmed wrote: > > 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 <yosryahmed@google.com> > > --- > > .../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; > > > The code below looks correct, but can be simplified a bit. > And btw thank you for adding a test! > > Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev> > (idk if you want invest your time in further simplication of this code, > it was this way before this patch, so up to you). I don't "want" to, but the voices in my head won't shut up until I do so.. Here's a patch that simplifies the code, I inlined it here to avoid sending a new version. If it looks good to you, it can be squashed into this patch or merged separately (whatever you and Andrew prefer). I can also send it in a separate thread if preferred. From 18c40d61dac05b33cfc9233b17979b54422ed7c5 Mon Sep 17 00:00:00 2001 From: Yosry Ahmed <yosryahmed@google.com> Date: Thu, 24 Nov 2022 02:21:12 +0000 Subject: [PATCH] selftests: cgroup: simplify memcg reclaim code Simplify the code for the reclaim_until() helper used for memcg reclaim through memory.reclaim. Signed-off-by: Yosry Ahmed <yosryahmed@google.com> --- .../selftests/cgroup/test_memcontrol.c | 65 ++++++++++--------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index bac3b91f1579..2e2bde44a6f7 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -17,6 +17,7 @@ #include <netdb.h> #include <errno.h> #include <sys/mman.h> +#include <limits.h> #include "../kselftest.h" #include "cgroup_util.h" @@ -656,51 +657,51 @@ static int test_memcg_max(const char *root) return ret; } -/* Reclaim from @memcg until usage reaches @goal_usage */ +/* + * Reclaim from @memcg until usage reaches @goal_usage 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_usage) { char buf[64]; int retries = 5; - int err; + int err = INT_MAX; 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; + /* First iteration*/ + if (err == INT_MAX) { + if (current <= goal_usage) + return false; + /* Write successful, check reclaimed amount */ + } else if (!err) { + return current <= goal_usage || + values_close(current, goal_usage, 3); + /* Unexpected error, or ran out of retries */ + } else if (err != -EAGAIN || !retries--) { + return false; + /* EAGAIN -> retry, but check for false negatives */ + } else if (current <= goal_usage) { + return false; + } + to_reclaim = current - goal_usage; 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; } /* -- 2.38.1.584.g0f3c55d4c2-goog > > > } > > > > +/* 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; > > There are 3 places in this function where memory.current is read and compared > to goal_usage. I believe only one can be left. > > > + 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; > > if (err != -EAGAIN || retries--) > break; > > Thanks!
On Wed, Nov 23, 2022 at 7:16 PM Yosry Ahmed <yosryahmed@google.com> wrote: > > On Wed, Nov 23, 2022 at 5:03 PM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > > > On Wed, Nov 23, 2022 at 09:21:31AM +0000, Yosry Ahmed wrote: > > > 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 <yosryahmed@google.com> > > > --- > > > .../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; > > > > > > The code below looks correct, but can be simplified a bit. > > And btw thank you for adding a test! > > > > Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev> > > (idk if you want invest your time in further simplication of this code, > > it was this way before this patch, so up to you). > > I don't "want" to, but the voices in my head won't shut up until I do so.. > > Here's a patch that simplifies the code, I inlined it here to avoid > sending a new version. If it looks good to you, it can be squashed > into this patch or merged separately (whatever you and Andrew prefer). > I can also send it in a separate thread if preferred. Roman, any thoughts on this? > > > From 18c40d61dac05b33cfc9233b17979b54422ed7c5 Mon Sep 17 00:00:00 2001 > From: Yosry Ahmed <yosryahmed@google.com> > Date: Thu, 24 Nov 2022 02:21:12 +0000 > Subject: [PATCH] selftests: cgroup: simplify memcg reclaim code > > Simplify the code for the reclaim_until() helper used for memcg reclaim > through memory.reclaim. > > Signed-off-by: Yosry Ahmed <yosryahmed@google.com> > --- > .../selftests/cgroup/test_memcontrol.c | 65 ++++++++++--------- > 1 file changed, 33 insertions(+), 32 deletions(-) > > diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c > b/tools/testing/selftests/cgroup/test_memcontrol.c > index bac3b91f1579..2e2bde44a6f7 100644 > --- a/tools/testing/selftests/cgroup/test_memcontrol.c > +++ b/tools/testing/selftests/cgroup/test_memcontrol.c > @@ -17,6 +17,7 @@ > #include <netdb.h> > #include <errno.h> > #include <sys/mman.h> > +#include <limits.h> > > #include "../kselftest.h" > #include "cgroup_util.h" > @@ -656,51 +657,51 @@ static int test_memcg_max(const char *root) > return ret; > } > > -/* Reclaim from @memcg until usage reaches @goal_usage */ > +/* > + * Reclaim from @memcg until usage reaches @goal_usage 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_usage) > { > char buf[64]; > int retries = 5; > - int err; > + int err = INT_MAX; > 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; > + /* First iteration*/ > + if (err == INT_MAX) { > + if (current <= goal_usage) > + return false; > + /* Write successful, check reclaimed amount */ > + } else if (!err) { > + return current <= goal_usage || > + values_close(current, goal_usage, 3); > + /* Unexpected error, or ran out of retries */ > + } else if (err != -EAGAIN || !retries--) { > + return false; > + /* EAGAIN -> retry, but check for false negatives */ > + } else if (current <= goal_usage) { > + return false; > + } > > + to_reclaim = current - goal_usage; > 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; > } > > /* > -- > 2.38.1.584.g0f3c55d4c2-goog > > > > > > } > > > > > > +/* 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; > > > > There are 3 places in this function where memory.current is read and compared > > to goal_usage. I believe only one can be left. > > > > > + 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; > > > > if (err != -EAGAIN || retries--) > > break; > > > > Thanks!
On Tue, Nov 29, 2022 at 11:42:31AM -0800, Yosry Ahmed wrote: > On Wed, Nov 23, 2022 at 7:16 PM Yosry Ahmed <yosryahmed@google.com> wrote: > > > > On Wed, Nov 23, 2022 at 5:03 PM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > > > > > On Wed, Nov 23, 2022 at 09:21:31AM +0000, Yosry Ahmed wrote: > > > > 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 <yosryahmed@google.com> > > > > --- > > > > .../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; > > > > > > > > > The code below looks correct, but can be simplified a bit. > > > And btw thank you for adding a test! > > > > > > Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev> > > > (idk if you want invest your time in further simplication of this code, > > > it was this way before this patch, so up to you). > > > > I don't "want" to, but the voices in my head won't shut up until I do so.. > > > > Here's a patch that simplifies the code, I inlined it here to avoid > > sending a new version. If it looks good to you, it can be squashed > > into this patch or merged separately (whatever you and Andrew prefer). > > I can also send it in a separate thread if preferred. > > Roman, any thoughts on this? > > > > > > > From 18c40d61dac05b33cfc9233b17979b54422ed7c5 Mon Sep 17 00:00:00 2001 > > From: Yosry Ahmed <yosryahmed@google.com> > > Date: Thu, 24 Nov 2022 02:21:12 +0000 > > Subject: [PATCH] selftests: cgroup: simplify memcg reclaim code > > > > Simplify the code for the reclaim_until() helper used for memcg reclaim > > through memory.reclaim. > > > > Signed-off-by: Yosry Ahmed <yosryahmed@google.com> > > --- > > .../selftests/cgroup/test_memcontrol.c | 65 ++++++++++--------- > > 1 file changed, 33 insertions(+), 32 deletions(-) > > > > diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c > > b/tools/testing/selftests/cgroup/test_memcontrol.c > > index bac3b91f1579..2e2bde44a6f7 100644 > > --- a/tools/testing/selftests/cgroup/test_memcontrol.c > > +++ b/tools/testing/selftests/cgroup/test_memcontrol.c > > @@ -17,6 +17,7 @@ > > #include <netdb.h> > > #include <errno.h> > > #include <sys/mman.h> > > +#include <limits.h> > > > > #include "../kselftest.h" > > #include "cgroup_util.h" > > @@ -656,51 +657,51 @@ static int test_memcg_max(const char *root) > > return ret; > > } > > > > -/* Reclaim from @memcg until usage reaches @goal_usage */ > > +/* > > + * Reclaim from @memcg until usage reaches @goal_usage 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_usage) > > { > > char buf[64]; > > int retries = 5; > > - int err; > > + int err = INT_MAX; > > long current, to_reclaim; > > > > - /* Nothing to do here */ > > - if (cg_read_long(memcg, "memory.current") <= goal_usage) > > - return true; > > - Hi Yosry! Thank you for working on this! I feel like it's still way more complex than it can be. How about something like this? (completely untested, treat is as a pseudo-code). { ... bool ret = false; for (retries = 5; retries > 0; retries--) { current = cg_read_long(memcg, "memory.current"); if (current <= goal) // replace with values_close? break; to_reclaim = current - goal_usage; snprintf(buf, sizeof(buf), "%ld", to_reclaim); err = cg_write(memcg, "memory.reclaim", buf); if (!err) ret = true; else if (err != -AGAIN) break; } return ret; }
On Wed, Nov 30, 2022 at 9:20 AM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > On Tue, Nov 29, 2022 at 11:42:31AM -0800, Yosry Ahmed wrote: > > On Wed, Nov 23, 2022 at 7:16 PM Yosry Ahmed <yosryahmed@google.com> wrote: > > > > > > On Wed, Nov 23, 2022 at 5:03 PM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > > > > > > > On Wed, Nov 23, 2022 at 09:21:31AM +0000, Yosry Ahmed wrote: > > > > > 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 <yosryahmed@google.com> > > > > > --- > > > > > .../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; > > > > > > > > > > > > The code below looks correct, but can be simplified a bit. > > > > And btw thank you for adding a test! > > > > > > > > Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev> > > > > (idk if you want invest your time in further simplication of this code, > > > > it was this way before this patch, so up to you). > > > > > > I don't "want" to, but the voices in my head won't shut up until I do so.. > > > > > > Here's a patch that simplifies the code, I inlined it here to avoid > > > sending a new version. If it looks good to you, it can be squashed > > > into this patch or merged separately (whatever you and Andrew prefer). > > > I can also send it in a separate thread if preferred. > > > > Roman, any thoughts on this? > > > > > > > > > > > From 18c40d61dac05b33cfc9233b17979b54422ed7c5 Mon Sep 17 00:00:00 2001 > > > From: Yosry Ahmed <yosryahmed@google.com> > > > Date: Thu, 24 Nov 2022 02:21:12 +0000 > > > Subject: [PATCH] selftests: cgroup: simplify memcg reclaim code > > > > > > Simplify the code for the reclaim_until() helper used for memcg reclaim > > > through memory.reclaim. > > > > > > Signed-off-by: Yosry Ahmed <yosryahmed@google.com> > > > --- > > > .../selftests/cgroup/test_memcontrol.c | 65 ++++++++++--------- > > > 1 file changed, 33 insertions(+), 32 deletions(-) > > > > > > diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c > > > b/tools/testing/selftests/cgroup/test_memcontrol.c > > > index bac3b91f1579..2e2bde44a6f7 100644 > > > --- a/tools/testing/selftests/cgroup/test_memcontrol.c > > > +++ b/tools/testing/selftests/cgroup/test_memcontrol.c > > > @@ -17,6 +17,7 @@ > > > #include <netdb.h> > > > #include <errno.h> > > > #include <sys/mman.h> > > > +#include <limits.h> > > > > > > #include "../kselftest.h" > > > #include "cgroup_util.h" > > > @@ -656,51 +657,51 @@ static int test_memcg_max(const char *root) > > > return ret; > > > } > > > > > > -/* Reclaim from @memcg until usage reaches @goal_usage */ > > > +/* > > > + * Reclaim from @memcg until usage reaches @goal_usage 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_usage) > > > { > > > char buf[64]; > > > int retries = 5; > > > - int err; > > > + int err = INT_MAX; > > > long current, to_reclaim; > > > > > > - /* Nothing to do here */ > > > - if (cg_read_long(memcg, "memory.current") <= goal_usage) > > > - return true; > > > - > > Hi Yosry! > > Thank you for working on this! > I feel like it's still way more complex than it can be. > How about something like this? (completely untested, treat is > as a pseudo-code). Thanks Roman! This looks much simpler, and it nicely and subtly catches the false negative case (where we return -EAGAIN but have actually reclaimed the requested amount), but I think it doesn't catch the false positive case (where memory.reclaim returns 0 but hasn't reclaimed enough memory). In this case I think we will just keep retrying and ignore the false positive? Maybe with the following added check? > > > { > ... > bool ret = false; > > for (retries = 5; retries > 0; retries--) { > current = cg_read_long(memcg, "memory.current"); > > if (current <= goal) // replace with values_close? > break; else if (ret) { // false positive? ret = false; break; } > > to_reclaim = current - goal_usage; > snprintf(buf, sizeof(buf), "%ld", to_reclaim); > err = cg_write(memcg, "memory.reclaim", buf); > if (!err) > ret = true; > else if (err != -AGAIN) > break; > } > > return ret; > } Also, please let me know if you prefer that I send this cleanup in the same thread like the above, in a completely separate patch that depends on this series, or have it squashed into this patch in a v3. Thanks again!
On Wed, Nov 30, 2022 at 10:25 AM Yosry Ahmed <yosryahmed@google.com> wrote: > > On Wed, Nov 30, 2022 at 9:20 AM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > > > On Tue, Nov 29, 2022 at 11:42:31AM -0800, Yosry Ahmed wrote: > > > On Wed, Nov 23, 2022 at 7:16 PM Yosry Ahmed <yosryahmed@google.com> wrote: > > > > > > > > On Wed, Nov 23, 2022 at 5:03 PM Roman Gushchin <roman.gushchin@linux.dev> wrote: > > > > > > > > > > On Wed, Nov 23, 2022 at 09:21:31AM +0000, Yosry Ahmed wrote: > > > > > > 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 <yosryahmed@google.com> > > > > > > --- > > > > > > .../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; > > > > > > > > > > > > > > > The code below looks correct, but can be simplified a bit. > > > > > And btw thank you for adding a test! > > > > > > > > > > Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev> > > > > > (idk if you want invest your time in further simplication of this code, > > > > > it was this way before this patch, so up to you). > > > > > > > > I don't "want" to, but the voices in my head won't shut up until I do so.. > > > > > > > > Here's a patch that simplifies the code, I inlined it here to avoid > > > > sending a new version. If it looks good to you, it can be squashed > > > > into this patch or merged separately (whatever you and Andrew prefer). > > > > I can also send it in a separate thread if preferred. > > > > > > Roman, any thoughts on this? > > > > > > > > > > > > > > > From 18c40d61dac05b33cfc9233b17979b54422ed7c5 Mon Sep 17 00:00:00 2001 > > > > From: Yosry Ahmed <yosryahmed@google.com> > > > > Date: Thu, 24 Nov 2022 02:21:12 +0000 > > > > Subject: [PATCH] selftests: cgroup: simplify memcg reclaim code > > > > > > > > Simplify the code for the reclaim_until() helper used for memcg reclaim > > > > through memory.reclaim. > > > > > > > > Signed-off-by: Yosry Ahmed <yosryahmed@google.com> > > > > --- > > > > .../selftests/cgroup/test_memcontrol.c | 65 ++++++++++--------- > > > > 1 file changed, 33 insertions(+), 32 deletions(-) > > > > > > > > diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c > > > > b/tools/testing/selftests/cgroup/test_memcontrol.c > > > > index bac3b91f1579..2e2bde44a6f7 100644 > > > > --- a/tools/testing/selftests/cgroup/test_memcontrol.c > > > > +++ b/tools/testing/selftests/cgroup/test_memcontrol.c > > > > @@ -17,6 +17,7 @@ > > > > #include <netdb.h> > > > > #include <errno.h> > > > > #include <sys/mman.h> > > > > +#include <limits.h> > > > > > > > > #include "../kselftest.h" > > > > #include "cgroup_util.h" > > > > @@ -656,51 +657,51 @@ static int test_memcg_max(const char *root) > > > > return ret; > > > > } > > > > > > > > -/* Reclaim from @memcg until usage reaches @goal_usage */ > > > > +/* > > > > + * Reclaim from @memcg until usage reaches @goal_usage 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_usage) > > > > { > > > > char buf[64]; > > > > int retries = 5; > > > > - int err; > > > > + int err = INT_MAX; > > > > long current, to_reclaim; > > > > > > > > - /* Nothing to do here */ > > > > - if (cg_read_long(memcg, "memory.current") <= goal_usage) > > > > - return true; > > > > - > > > > Hi Yosry! > > > > Thank you for working on this! > > I feel like it's still way more complex than it can be. > > How about something like this? (completely untested, treat is > > as a pseudo-code). > > Thanks Roman! > > This looks much simpler, and it nicely and subtly catches the false > negative case (where we return -EAGAIN but have actually reclaimed the > requested amount), but I think it doesn't catch the false positive > case (where memory.reclaim returns 0 but hasn't reclaimed enough > memory). In this case I think we will just keep retrying and ignore > the false positive? > > Maybe with the following added check? > > > > > > > { > > ... > > bool ret = false; > > > > for (retries = 5; retries > 0; retries--) { > > current = cg_read_long(memcg, "memory.current"); > > > > if (current <= goal) // replace with values_close? > > break; > else if (ret) { // false positive? > ret = false; > break; > } > > > > to_reclaim = current - goal_usage; > > snprintf(buf, sizeof(buf), "%ld", to_reclaim); > > err = cg_write(memcg, "memory.reclaim", buf); > > if (!err) > > ret = true; > > else if (err != -AGAIN) > > break; > > } > > > > return ret; > > } > > Also, please let me know if you prefer that I send this cleanup in the > same thread like the above, in a completely separate patch that > depends on this series, or have it squashed into this patch in a v3. > > Thanks again! I realized I missed a few folks in the CC of this version anyway. Sent v3 with the suggested refactoring (+ the missing check for false positives) squashed into this patch. Also included your review tags on patches 1 & 3 (patch 2 was almost rewritten according to your suggestions, so I dropped the review tag and added a suggested tag): https://lore.kernel.org/lkml/20221202031512.1365483-1-yosryahmed@google.com/ Thanks!
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: