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 68E97C4321E for ; Wed, 30 Nov 2022 18:26:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04E216B0072; Wed, 30 Nov 2022 13:26:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F3FE26B0073; Wed, 30 Nov 2022 13:26:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E08076B0074; Wed, 30 Nov 2022 13:26:26 -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 D09926B0072 for ; Wed, 30 Nov 2022 13:26:26 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7DC21160B27 for ; Wed, 30 Nov 2022 18:26:26 +0000 (UTC) X-FDA: 80190938772.15.86D18E6 Received: from mail-io1-f48.google.com (mail-io1-f48.google.com [209.85.166.48]) by imf25.hostedemail.com (Postfix) with ESMTP id 28983A0019 for ; Wed, 30 Nov 2022 18:26:24 +0000 (UTC) Received: by mail-io1-f48.google.com with SMTP id 135so1342037iou.7 for ; Wed, 30 Nov 2022 10:26:24 -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=sC8R1Y9mBKJ+sMKAdraa2B/xyNoHob+GAk8cNDVN2DQ=; b=FglytdjJtS+ckXnr291Bivorj0tAtiwmE0n/o1uVaPsluYB4mebgBuCAF/H7CghLLi miQyX+2Fi/BtOpQHv+593dIpeUbuVS6orOvFkHTtIqkqal4d7uqCzlgy36abMR7bp7CB XsbUytEy0sH+Le7Ii3Z5U02+/jw32dsndnm+ns2ZiKsZ4E726xl036CTbGbTfGLEtWN3 ozwFqixuiBFQ16rZ8vaJyl+eCco0rlfUJOpPuDcOzwV4IvHSwq/xbvKLRKjZd9vSQ66H f+D6Ke+TsQNevgdM3l9ai6lhumPXg3ye2hZp9oTJdBJjZmNMspQMlFdNXGuxLw9QOgyQ Dd0w== 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=sC8R1Y9mBKJ+sMKAdraa2B/xyNoHob+GAk8cNDVN2DQ=; b=Y0NW6/sV7MYTEWG/e9K+4yeGb4sW01R0nVQWs8qU8YJLc37WViCMCRVyZwDouqeFZq A1jUiVFRyrHBiCIvlLNV4GRBp1ZLGBmsfrUM1LQSA/M/ZIBhkJ9R3sUkQu0xsd71PFIV +O6+PerYETVg2DtOSbtZblaSpfChg8J2gM3JvGrbrgV5TUIx1N8TKPIQ9PmZoq87ivOH /j+eKK9WjDyC9UlHmCnzgR93FfGHGSwC4oX9vUz5GK9az+L5jc+ZlXhP/F7/0bGqYn68 wq/VW5hba8PWfFzeOr5cQfWdGsmrn531TyJKcBTqaMi1g3i5R2ma0Rt3jxMp1nz5xRfK 3CyQ== X-Gm-Message-State: ANoB5pnXGoMhd6RVbHpKfH8IVrjuSCPd90yM+dySOM6SuOTccuFag0QK wj1kKYBaahoOYA9RJi8h7t9QcGxkxgd/ecptPJRuhQ== X-Google-Smtp-Source: AA0mqf7XlxXtau17W+8+HwXEFxta7c4YtY5lg4v+iHU3yqkgS62M58ZyVuvznyfnsqEOOFxe13EYkY0aQnxO802687E= X-Received: by 2002:a05:6638:4716:b0:389:e336:e928 with SMTP id cs22-20020a056638471600b00389e336e928mr7405125jab.51.1669832784172; Wed, 30 Nov 2022 10:26:24 -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, 30 Nov 2022 10:25:48 -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; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=FglytdjJ; spf=pass (imf25.hostedemail.com: domain of yosryahmed@google.com designates 209.85.166.48 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=1669832785; a=rsa-sha256; cv=none; b=sKGukv2PklxnV4CoKPiXsBEMfZODwWrKXlnAgub4yx994pyNRTg4z+ZF16Dn1X0GodZ0G5 GNYl1oTt3Ax6Ffra8relr4K0VMu3DFd8b4An3yNO8+zWEmv52Z119JdnTsxIt2+iJ1mmiE zib1YEaq0m1hpLWV/sEFUfp1XvlsKHc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669832785; 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=sC8R1Y9mBKJ+sMKAdraa2B/xyNoHob+GAk8cNDVN2DQ=; b=vnh7yuoDcraYnKPWz+fEFdGQlAHpKDutDVdIu0uflGeBg3gK+OrEOYJURkOgISk1DSM1A7 dLT3HMP0SWCo08KupWO18R/8peT5u2FZVSmE9Pp9m5N2SJoKR31evDsTGfWzKBzXwEN1uu iTFcN68x+R8qYEjK4wew3g6kZD0H+yw= Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=FglytdjJ; spf=pass (imf25.hostedemail.com: domain of yosryahmed@google.com designates 209.85.166.48 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 28983A0019 X-Stat-Signature: j13s6n4p9kh7u4coiwwfxr5mn83gj4t1 X-Rspam-User: X-HE-Tag: 1669832784-971891 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 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!