From patchwork Fri Feb 11 06:49:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12742927 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 BAB39C433EF for ; Fri, 11 Feb 2022 06:49:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 24ACB6B007B; Fri, 11 Feb 2022 01:49:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1FA8D6B007D; Fri, 11 Feb 2022 01:49:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09BC96B007E; Fri, 11 Feb 2022 01:49:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0235.hostedemail.com [216.40.44.235]) by kanga.kvack.org (Postfix) with ESMTP id EFF956B007B for ; Fri, 11 Feb 2022 01:49:52 -0500 (EST) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id A1DB6901C3 for ; Fri, 11 Feb 2022 06:49:52 +0000 (UTC) X-FDA: 79129573824.31.49D9D52 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf08.hostedemail.com (Postfix) with ESMTP id 3A5BD160008 for ; Fri, 11 Feb 2022 06:49:52 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id b12-20020a056902030c00b0061d720e274aso16944024ybs.20 for ; Thu, 10 Feb 2022 22:49:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=wSeYGbecgd4wcC85bjYc+Wf+OozOTN6ZFdjrqmxfx+w=; b=kgBNOs1WBseAbsRFMyzbv5MpUWXKAyzUAXftaQWeiVTYBMreznuOjzljFOI2hrN1yk aHQaCo+UhNqqwVRVTPBr7NM6NjjOEyVjuGyn+/MKv8P8ZNfn9x9nZJdD56raIihE0qyl cW+hk+/99lgXtJF1xLanqpAjJOO5g1YltZrYCaTDb+a2pqp7OAkhVEBR4eWnfXN+2ezT bDpg5N6KoEKtlQPiLB0AZhqLQR2bCasbsdIbjhbtS7a5XPCObdJ8Qbf6KIpdngCRFy4O yT/VmCbaWYaftIYqDgJzRfXpr9DXGJdeGzjvHXl9HegA/BTq6/R6BBz32ycj+VoCOw/2 UF1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=wSeYGbecgd4wcC85bjYc+Wf+OozOTN6ZFdjrqmxfx+w=; b=Mm0QP9T4AJdBoZv6LCSH6inON1qZq8eEv8aBI/JGjhwwtI7BNfDHc3ugCvXlea3gso 4O30xlpfFSLd5sFOZHtubtSVVrp5Q2+JSofbzo8mDk3IRBAn82OLioe5M4CoOUGoKh7I z1vBmAysm61aL2wuR6hy3enqHerhIWiqsFT0xNkTAyAJx1D/BnKrFgE71R/jcbAQi4eT NU+Jp+Y1zZx57Exg3noNzylHiycKkQmuM7B37f68WnXi8uSLqZPoxP6u4L/8GDI9g98M cUoRpgdfpLEBOEvHnB7XGOSp17dEHKjZQcDa3JQqvuYvR+XEKVFFkBF9j+SJ4+jmJ2v4 /AmA== X-Gm-Message-State: AOAM533E3gTSVcFA/L05M3FYVlJscz16he5adUBwVeY4Ic24wIinb7/k mkNpcwqD1uiBQm1L7EJYvFms7kzyGyJjEA== X-Google-Smtp-Source: ABdhPJwKpmkurBJZDlS/fuYlvpdKg9WPvDaf+agCb8KXEOQoRBet3SsqP1VSya2bsK2552ZipDH4CRMFgYBezw== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:9a07:ef1a:2fee:57f1]) (user=shakeelb job=sendgmr) by 2002:a81:a304:: with SMTP id a4mr337510ywh.516.1644562191536; Thu, 10 Feb 2022 22:49:51 -0800 (PST) Date: Thu, 10 Feb 2022 22:49:16 -0800 In-Reply-To: <20220211064917.2028469-1-shakeelb@google.com> Message-Id: <20220211064917.2028469-4-shakeelb@google.com> Mime-Version: 1.0 References: <20220211064917.2028469-1-shakeelb@google.com> X-Mailer: git-send-email 2.35.1.265.g69c8d7142f-goog Subject: [PATCH v2 3/4] selftests: memcg: test high limit for single entry allocation From: Shakeel Butt To: Johannes Weiner , Michal Hocko , Roman Gushchin Cc: Chris Down , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kgBNOs1W; spf=pass (imf08.hostedemail.com: domain of 3DwcGYggKCEw6voysszpu22uzs.q20zw18B-00y9oqy.25u@flex--shakeelb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3DwcGYggKCEw6voysszpu22uzs.q20zw18B-00y9oqy.25u@flex--shakeelb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam07 X-Rspam-User: X-Rspamd-Queue-Id: 3A5BD160008 X-Stat-Signature: b1q9pxzjsty9qu1nnu5u3qcik79qy5oy X-HE-Tag: 1644562192-545711 X-Bogosity: Ham, tests=bogofilter, spamicity=0.006606, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Test the enforcement of memory.high limit for large amount of memory allocation within a single kernel entry. There are valid use-cases where the application can trigger large amount of memory allocation within a single syscall e.g. mlock() or mmap(MAP_POPULATE). Make sure memory.high limit enforcement works for such use-cases. Signed-off-by: Shakeel Butt Reviewed-by: Roman Gushchin --- Changes since v1: - None tools/testing/selftests/cgroup/cgroup_util.c | 15 +++- tools/testing/selftests/cgroup/cgroup_util.h | 1 + .../selftests/cgroup/test_memcontrol.c | 78 +++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/cgroup_util.c index 0cf7e90c0052..dbaa7aabbb4a 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.c +++ b/tools/testing/selftests/cgroup/cgroup_util.c @@ -583,7 +583,7 @@ int clone_into_cgroup_run_wait(const char *cgroup) return 0; } -int cg_prepare_for_wait(const char *cgroup) +static int __prepare_for_wait(const char *cgroup, const char *filename) { int fd, ret = -1; @@ -591,8 +591,7 @@ int cg_prepare_for_wait(const char *cgroup) if (fd == -1) return fd; - ret = inotify_add_watch(fd, cg_control(cgroup, "cgroup.events"), - IN_MODIFY); + ret = inotify_add_watch(fd, cg_control(cgroup, filename), IN_MODIFY); if (ret == -1) { close(fd); fd = -1; @@ -601,6 +600,16 @@ int cg_prepare_for_wait(const char *cgroup) return fd; } +int cg_prepare_for_wait(const char *cgroup) +{ + return __prepare_for_wait(cgroup, "cgroup.events"); +} + +int memcg_prepare_for_wait(const char *cgroup) +{ + return __prepare_for_wait(cgroup, "memory.events"); +} + int cg_wait_for(int fd) { int ret = -1; diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/selftests/cgroup/cgroup_util.h index 4f66d10626d2..628738532ac9 100644 --- a/tools/testing/selftests/cgroup/cgroup_util.h +++ b/tools/testing/selftests/cgroup/cgroup_util.h @@ -55,4 +55,5 @@ extern int clone_reap(pid_t pid, int options); extern int clone_into_cgroup_run_wait(const char *cgroup); extern int dirfd_open_opath(const char *dir); extern int cg_prepare_for_wait(const char *cgroup); +extern int memcg_prepare_for_wait(const char *cgroup); extern int cg_wait_for(int fd); diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index c19a97dd02d4..36ccf2322e21 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "../kselftest.h" #include "cgroup_util.h" @@ -628,6 +629,82 @@ static int test_memcg_high(const char *root) return ret; } +static int alloc_anon_mlock(const char *cgroup, void *arg) +{ + size_t size = (size_t)arg; + void *buf; + + buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, + 0, 0); + if (buf == MAP_FAILED) + return -1; + + mlock(buf, size); + munmap(buf, size); + return 0; +} + +/* + * This test checks that memory.high is able to throttle big single shot + * allocation i.e. large allocation within one kernel entry. + */ +static int test_memcg_high_sync(const char *root) +{ + int ret = KSFT_FAIL, pid, fd = -1; + char *memcg; + long pre_high, pre_max; + long post_high, post_max; + + memcg = cg_name(root, "memcg_test"); + if (!memcg) + goto cleanup; + + if (cg_create(memcg)) + goto cleanup; + + pre_high = cg_read_key_long(memcg, "memory.events", "high "); + pre_max = cg_read_key_long(memcg, "memory.events", "max "); + if (pre_high < 0 || pre_max < 0) + goto cleanup; + + if (cg_write(memcg, "memory.swap.max", "0")) + goto cleanup; + + if (cg_write(memcg, "memory.high", "30M")) + goto cleanup; + + if (cg_write(memcg, "memory.max", "140M")) + goto cleanup; + + fd = memcg_prepare_for_wait(memcg); + if (fd < 0) + goto cleanup; + + pid = cg_run_nowait(memcg, alloc_anon_mlock, (void *)MB(200)); + if (pid < 0) + goto cleanup; + + cg_wait_for(fd); + + post_high = cg_read_key_long(memcg, "memory.events", "high "); + post_max = cg_read_key_long(memcg, "memory.events", "max "); + if (post_high < 0 || post_max < 0) + goto cleanup; + + if (pre_high == post_high || pre_max != post_max) + goto cleanup; + + ret = KSFT_PASS; + +cleanup: + if (fd >= 0) + close(fd); + cg_destroy(memcg); + free(memcg); + + return ret; +} + /* * This test checks that memory.max limits the amount of * memory which can be consumed by either anonymous memory @@ -1180,6 +1257,7 @@ struct memcg_test { T(test_memcg_min), T(test_memcg_low), T(test_memcg_high), + T(test_memcg_high_sync), T(test_memcg_max), T(test_memcg_oom_events), T(test_memcg_swap_max),