From mboxrd@z Thu Jan 1 00:00:00 1970 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 BC4B4C4332F for ; Fri, 2 Dec 2022 03:20:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4317D6B0073; Thu, 1 Dec 2022 22:20:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E2106B0074; Thu, 1 Dec 2022 22:20:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 283E96B0075; Thu, 1 Dec 2022 22:20:04 -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 197036B0073 for ; Thu, 1 Dec 2022 22:20:04 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id DC230AB214 for ; Fri, 2 Dec 2022 03:20:03 +0000 (UTC) X-FDA: 80195912286.03.2E3E0F8 Received: from mail-io1-f45.google.com (mail-io1-f45.google.com [209.85.166.45]) by imf12.hostedemail.com (Postfix) with ESMTP id 8BF4040013 for ; Fri, 2 Dec 2022 03:20:02 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=PBjCENkq; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of yosryahmed@google.com designates 209.85.166.45 as permitted sender) smtp.mailfrom=yosryahmed@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669951202; 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=AQDzzFTey2VIGmW/220/e2ZM1PaWhE7HKtjmhjomFW0=; b=wUIj5brH/jEkaU9Vx+Zhar/mIFhUYgx64BI1IAvCJgVWm8GWYNV8t4z9eqVYWDDTgwFSO7 BByUuccWmAfWgWMzqPtY/5iOCab+64AbL3N17j4T4BUjeTRHH/GeYs0ilgTPBWyUw4w5li CcVsJmoMmGOam4WtgEWsoXMIaRS/HlM= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=PBjCENkq; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of yosryahmed@google.com designates 209.85.166.45 as permitted sender) smtp.mailfrom=yosryahmed@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669951202; a=rsa-sha256; cv=none; b=mNt0BDp/8u1Prcicd2Y0T+efDZPD17Ap8i+Pu5Qql5zRceCauZzY3mQzBmnZfj22gDgrIf FCiVwZgNAtAgva83dLpFdEQUYaZpE59a2ynZPSEovoMOBvgGogmGkR7RSH5FWosXVPNUSq W/YufNblOXYx/iYoH1BVazJF8Zqid/E= Received: by mail-io1-f45.google.com with SMTP id 135so2363753iou.7 for ; Thu, 01 Dec 2022 19:20:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=AQDzzFTey2VIGmW/220/e2ZM1PaWhE7HKtjmhjomFW0=; b=PBjCENkqqjop+mquSyQ0nD7UGdDUXFMJusOPayg4nfDUlK5skhdQqWBrYCBm6CbFfQ YJLZcT8Y1qpcHT2u1fa0LR9nCnBjLzVLqWMmlBvCkjSBd5iLCsMPZUdnaswKw2i8rib/ 4XKp4PK/ruJ/dnej/0CsGEw8SileKUnJyXCSwKtUmWjauUIKUaFYVAg5L3dOo7xDsMww TQ5ay+Z8cwHlZLtn8SD8mIXCFNek6U0U0xZJ2i0R96zxG81Y79ocFvYj2wk1/HI/H9/O 6Z1gkyHFot/uxEGb6dJB81U7nzSc6mP33lm8THcrt0wfr2dlXoWYtvOHQ+5xvKzgeCy1 YcBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AQDzzFTey2VIGmW/220/e2ZM1PaWhE7HKtjmhjomFW0=; b=JwU4P5yPHatbxw8Z6VZ1nFpbFaD5RCFXpgeVKfB4Pmp3jar1pPWheeAzNgcAsrFnxj vgczswxcB2ou0RYJ3EGVF1dqEPSKLonVRMOcXinNpo9o93P2WZDHmWzKbFBK984z03w/ Xhz4XuTNqldPtbrOicPwCL2EAzfgu1x+1opyzcwdZUhoDS03RNrFkvDTqyloGujSOikT uEKQ+eeFsiSDzpEQ6h55bxln/xEbRKwJccx11Cxy1IiEEk6P1PkFZbtzHdpvLLfUCeW+ Tj7A2GlS49M1EgiqK7aifC0+9ltw8X+JrzoGCIln/kkzCnBBHtZIvMF8ccylKeTUX8iy Ekpg== X-Gm-Message-State: ANoB5pkzjRvITFTxTJdGCqWlrfUwW53kTwmz06dy3TgWMIgNYoMEHLkx p+WTOtVvIb4OnX1GCkXGhyj37uO6r84k1fuJ6EtgxA== X-Google-Smtp-Source: AA0mqf477Cau7AFPlNqKnoeqWGMDtb3WZlp02LyiEpUK95rhWqW0JZl7MJEmy5lrwU1gpktz7Jy7hj7pilJYdDztb9s= X-Received: by 2002:a6b:4409:0:b0:6de:bd7d:ee08 with SMTP id r9-20020a6b4409000000b006debd7dee08mr23598092ioa.0.1669951201609; Thu, 01 Dec 2022 19:20:01 -0800 (PST) MIME-Version: 1.0 References: <20221123092132.2521764-1-yosryahmed@google.com> <20221123092132.2521764-3-yosryahmed@google.com> In-Reply-To: From: Yosry Ahmed Date: Thu, 1 Dec 2022 19:19:25 -0800 Message-ID: Subject: Re: [PATCH v2 2/3] selftests: cgroup: refactor proactive reclaim code to reclaim_until() To: Roman Gushchin Cc: Shakeel Butt , Johannes Weiner , Michal Hocko , Yu Zhao , Muchun Song , "Matthew Wilcox (Oracle)" , Vasily Averin , Vlastimil Babka , Chris Down , linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-Spamd-Result: default: False [1.10 / 9.00]; SORBS_IRL_BL(3.00)[209.85.166.45:from]; BAYES_HAM(-3.00)[100.00%]; SUBJECT_HAS_UNDERSCORES(1.00)[]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; TO_DN_SOME(0.00)[]; R_DKIM_ALLOW(0.00)[google.com:s=20210112]; RCPT_COUNT_TWELVE(0.00)[12]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; MIME_TRACE(0.00)[0:+]; R_SPF_ALLOW(0.00)[+ip4:209.85.128.0/17]; DMARC_POLICY_ALLOW(0.00)[google.com,reject]; TO_MATCH_ENVRCPT_SOME(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; DKIM_TRACE(0.00)[google.com:+]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; FROM_HAS_DN(0.00)[]; ARC_NA(0.00)[] X-Stat-Signature: oq5yx6pzw8rondctggp7anfdq7we6q3h X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 8BF4040013 X-Rspam-User: X-HE-Tag: 1669951202-744413 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: On Wed, Nov 30, 2022 at 10:25 AM Yosry Ahmed wrote: > > On Wed, Nov 30, 2022 at 9:20 AM Roman Gushchin 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 wrote: > > > > > > > > On Wed, Nov 23, 2022 at 5:03 PM Roman Gushchin 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 > > > > > > --- > > > > > > .../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 > > > > > (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 > > > > 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 > > > > --- > > > > .../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 > > > > #include > > > > #include > > > > +#include > > > > > > > > #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!