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 00DF7C4332F for ; Thu, 24 Nov 2022 03:17:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 30BA16B0071; Wed, 23 Nov 2022 22:17:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2BB4E6B0072; Wed, 23 Nov 2022 22:17:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 182E56B0074; Wed, 23 Nov 2022 22:17:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 05AD36B0071 for ; Wed, 23 Nov 2022 22:17:32 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C63BB40110 for ; Thu, 24 Nov 2022 03:17:31 +0000 (UTC) X-FDA: 80166875502.28.2768604 Received: from mail-io1-f46.google.com (mail-io1-f46.google.com [209.85.166.46]) by imf24.hostedemail.com (Postfix) with ESMTP id 7C29F18000D for ; Thu, 24 Nov 2022 03:17:31 +0000 (UTC) Received: by mail-io1-f46.google.com with SMTP id e189so477092iof.1 for ; Wed, 23 Nov 2022 19:17:31 -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=Fsj15DlIxi1p8BdOL0QgOxfY+9nESmZnVG6dxzy5A24=; b=eVMMQNnXhH1vKXeIWkE6RTFpv52GCC4QtJXcuw8sr9z4MUlPw+XZkBYj5HxCQV2JKf FPt4SB4mzsBdLJv3/L5Irl/m9w8xId3oqKbnQ2fLYTSXjkGT1ULCmKfYvFj+NwQ10RKI qDUPLKzD/446jpylZISgHV2dipvq8GHjsgnP7fhIcinaMre615j9Ful/xRpw3q8bxYQb kxFD6mZQWYIVHQxvMF/Gfbsrdccps3M6fd9IsHHNxvLz+ji0CXLYQbRRe3Lz6UT8RaKP ZZ7FSRgNc6EI06d3lyLTSg8FX1IAl0/HYxRX1s7v9y90/IvgIsxVYnRyh2g9NaFbYzze PPTw== 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=Fsj15DlIxi1p8BdOL0QgOxfY+9nESmZnVG6dxzy5A24=; b=AGConSCw2jvCwgz7Rh6X+CmCeRdS44b3p2YW9f6JPk8FcyU9zuyxlETsAgDHx2fi++ OEoScUgR9O/hOG4RFQIM5W7qjPAgxT9COQcnra25+NWYtuiKuOusfOehmPbzJnAPJgWD nJRgLjB0D4xrDFoLbMpiAUIr0X80ary+TBQUjGCjJVnEpknD667lnsKtTE1dhYC9L52n CmLFJJ51GaJXRr31sSDWEwL+cihML/TVFCQl4pHw1JYtY1kVS0RklaosqWdLbCdibnin fMtG4uI/9Lryu8valk1gk5Zz6aRnH3kyrWMOCMdImk4zdhkcBVe2qgm+gftR2KHiLSE8 BmzA== X-Gm-Message-State: ANoB5plG9vVXRtKb53BwPArhAhauvIYcLPgVLy6Pl+mm6sH3zDbTwNzj e4PSIC5Z52uNJVmLGi5XU7r+SiIY9Mc5HpLQjpgJgQ== X-Google-Smtp-Source: AA0mqf7K42pA8O0tKKrne5Z7+/bPfH3u54TiV5CXl6bL2+TSMkxjiy3i1LClciRHhxZ0Voo5vxj+u9Rat3Fz/bnoGzU= X-Received: by 2002:a02:9422:0:b0:373:2c18:a37e with SMTP id a31-20020a029422000000b003732c18a37emr14119216jai.51.1669259850632; Wed, 23 Nov 2022 19:17:30 -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: Wed, 23 Nov 2022 19:16:54 -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" ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=eVMMQNnX; spf=pass (imf24.hostedemail.com: domain of yosryahmed@google.com designates 209.85.166.46 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669259851; a=rsa-sha256; cv=none; b=F6NaeEnD9lJUWySUjwP6kqOnjca5DdeR99PuCg71HtYLIAH2dtRciR3OXus3SKJ1mIgNrb s1dDNEHYHIQwdW95NIhZcbE1/JUc626u0vD5AS8iaug2jpNI12dezCQwEoxCYHsryIUpq5 WXFGvRTT6WndKV8W8Zkf4EU4HnPYHfY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669259851; 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=Fsj15DlIxi1p8BdOL0QgOxfY+9nESmZnVG6dxzy5A24=; b=2COPkafTazWtNvByMAYBc88fl4y4T/61jcXp12mjZnT34zqdaqLarp4/lvxUsw0rJzWn+D W0M3aqu5E6aP+Uoh229JvYawX/oV8MSK0dBhsfsNuTpWCkwQ4xZuBtJV0JyX2BzJfJkgxL v9/vSgge0eve7UKU2GxlrUhSX/grLCY= X-Rspam-User: Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=eVMMQNnX; spf=pass (imf24.hostedemail.com: domain of yosryahmed@google.com designates 209.85.166.46 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: g4djm8if8r5nr37mexbasip4a3whznx7 X-Rspamd-Queue-Id: 7C29F18000D X-Rspamd-Server: rspam09 X-HE-Tag: 1669259851-825819 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 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. >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; - 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!