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 4E5B2C4332F for ; Thu, 24 Nov 2022 01:03:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE88A6B0071; Wed, 23 Nov 2022 20:03:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D98C06B0072; Wed, 23 Nov 2022 20:03:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C38E86B0074; Wed, 23 Nov 2022 20:03:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B68F86B0071 for ; Wed, 23 Nov 2022 20:03:47 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 94D9C121188 for ; Thu, 24 Nov 2022 01:03:47 +0000 (UTC) X-FDA: 80166538494.26.3319A90 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf10.hostedemail.com (Postfix) with ESMTP id F3081C0011 for ; Thu, 24 Nov 2022 01:03:46 +0000 (UTC) Date: Wed, 23 Nov 2022 17:03:39 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1669251824; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ybudtwMfrOEChaCHm/veUGjVUgd9li4v5+FEt3DuvAM=; b=pTarRRWK45RONZmknVKroXtsrcCWE3jKtM2/fSHNvSdWwUev8stYRSueY2nyn8bmvjMkKR SJNLV/DsV2Ku6LKy2xd3bb/BcTTKaVD7FB+Ntfh+qu/7aHOv1bBEupFrRMrHheIWcSHBHC nOgRwDiWla4t6f0l2ke/V/XuUBYNBw8= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Yosry Ahmed 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 Subject: Re: [PATCH v2 2/3] selftests: cgroup: refactor proactive reclaim code to reclaim_until() Message-ID: References: <20221123092132.2521764-1-yosryahmed@google.com> <20221123092132.2521764-3-yosryahmed@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221123092132.2521764-3-yosryahmed@google.com> X-Migadu-Flow: FLOW_OUT ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669251827; a=rsa-sha256; cv=none; b=UOdZC4XNkX/jNsSSEiFhN7KJWdLYJdtG//9NGzwcOfClKVUzt+Hjcvs32iunz6D4TS9u6Z 5TT822r07tCOkhlf0O9im9gRKmkRZLV/Hc6NLl2m0A4ZotFBjUpRsn5vFvGyOsxCRnTfhn o8Xpuzp1kK50b0wRLQaS3ndgaCt+Szk= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pTarRRWK; spf=pass (imf10.hostedemail.com: domain of roman.gushchin@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669251827; 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=ybudtwMfrOEChaCHm/veUGjVUgd9li4v5+FEt3DuvAM=; b=73W9v+PFF0XjnA/Vpb1bwhhEblOzuzuxqCQuhZWxs7AgwtlBOyHWz33mEtHi1cKEQGvQeF /sjWorm67Uj3ZsebLaOLIH8w9c9RYELiJ8jOke5CkOIKH+0Yn/dFWm/immn9Np0NmIfXdy FvUGxKr874UZG/d4sXWjRLoF557NAzM= X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: F3081C0011 Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pTarRRWK; spf=pass (imf10.hostedemail.com: domain of roman.gushchin@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-Stat-Signature: czft4ga6tgugs4bf31szxmyx4pbwup1n X-HE-Tag: 1669251826-439141 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 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). > } > > +/* 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!