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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8EC40C433F5 for ; Wed, 10 Nov 2021 21:20:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 75B2C611F2 for ; Wed, 10 Nov 2021 21:20:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233427AbhKJVW5 (ORCPT ); Wed, 10 Nov 2021 16:22:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233433AbhKJVW4 (ORCPT ); Wed, 10 Nov 2021 16:22:56 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30F51C06127A for ; Wed, 10 Nov 2021 13:20:08 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id e12-20020aa7980c000000b0049fa3fc29d0so2646808pfl.10 for ; Wed, 10 Nov 2021 13:20:08 -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:cc; bh=XWUyVcqFDorfkxK8BmUlQ2jLw5lcWmVuPPsoovex71Y=; b=G2nMJLGxPIMyEeUfb/vaasX85o5Sfnbuzq+2YEd/YK686uuZTNaruqnosc1t/nwbaF m5N05wjs10IyzymZE1gJ6SWhZ1AMTcdNqaVe9/ePQ2lp28a0MtFXaJFaLSD8rvEzo5Wh arhruu4iKXYXKUfbZPe5gNtak87IS9WUPyWzKzMJN8Yxwl5hewD/kaNYM4lIxyeTf+fa nBx6tWsFO5qj9ltm0CS79xcUMAQAurs6zyFOmzCmBBqH4fRRF/TfEkkeKLL+FAEsaSzs RtO8Yf1co/HOZQINwy2CIqTxLQgBoMSO3o/5uxwPDjYidsajo4DRAO7ZTOxHZhxVZTK5 Ornw== 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:cc; bh=XWUyVcqFDorfkxK8BmUlQ2jLw5lcWmVuPPsoovex71Y=; b=C7PdMTNFgCdYpsfgssAV3vYo576p/Ceevq6zJIj17vuB4w+UnyfZgRi/OSQsAt4fqF JMWmpoOvsu+CUn0uYDVsAyvIz10NU14dFn3BvlfE6XUn4pIl2m2kD5yl6eEqDUYTdDNq NDtCL2YyYWoU1zZ1d/ERISPaQ2/zjVPcqbrx2AiEoLPrL7WHX1ImOwFpSwmXB/Q0FVEc UWCrfwMCpJtw52IBY2O0PjDoM6SwTEMrdvh4rELosZ5GTHyIEd1jnHtErxxin6GAfMgL qtbXrT6QO7jIbgRmgwAuoVRIMT8InAI0L4jexP8rGJw4nEQGZB059tXQphTfx0zwZwLo 4Oiw== X-Gm-Message-State: AOAM530ii/JRUGaLKn4VsDaJWGGglO9+ZxqyeToN2ldTByRGGiATfH8M lBano9dWNCBe90F5Mck86qyGjGSMMPidtPng8A== X-Google-Smtp-Source: ABdhPJwgK2AbRmOmsVL7fZ0uDB5Xq1S64WUY/kQU6kiEqA95TWLlPwwtuaHfqsCtshAjZpFdVr+GfBeRh9uNMzO7/w== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2cd:202:59c8:7b4e:e859:9db0]) (user=almasrymina job=sendgmr) by 2002:a62:4e87:0:b0:47b:dbbf:c6f0 with SMTP id c129-20020a624e87000000b0047bdbbfc6f0mr2005596pfb.47.1636579207325; Wed, 10 Nov 2021 13:20:07 -0800 (PST) Date: Wed, 10 Nov 2021 13:19:50 -0800 In-Reply-To: <20211110211951.3730787-1-almasrymina@google.com> Message-Id: <20211110211951.3730787-5-almasrymina@google.com> Mime-Version: 1.0 References: <20211110211951.3730787-1-almasrymina@google.com> X-Mailer: git-send-email 2.34.0.rc0.344.g81b53c2807-goog Subject: [PATCH v2 4/4] mm, shmem, selftests: add tmpfs memcg= mount option tests From: Mina Almasry Cc: Mina Almasry , Michal Hocko , "Theodore Ts'o" , Greg Thelen , Shakeel Butt , Andrew Morton , Hugh Dickins , Roman Gushchin , Dave Chinner , Johannes Weiner , Tejun Heo , Vladimir Davydov , Muchun Song , riel@surriel.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org Content-Type: text/plain; charset="UTF-8" To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Signed-off-by: Mina Almasry Cc: Michal Hocko Cc: Theodore Ts'o Cc: Greg Thelen Cc: Shakeel Butt Cc: Andrew Morton Cc: Hugh Dickins Cc: Roman Gushchin Cc: Dave Chinner Cc: Johannes Weiner Cc: Hugh Dickins Cc: Tejun Heo Cc: Vladimir Davydov Cc: Muchun Song Cc: riel@surriel.com Cc: linux-mm@kvack.org Cc: linux-fsdevel@vger.kernel.org Cc: cgroups@vger.kernel.org --- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/mmap_write.c | 103 ++++++++++++++++++++++ tools/testing/selftests/vm/tmpfs-memcg.sh | 87 ++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 tools/testing/selftests/vm/mmap_write.c create mode 100755 tools/testing/selftests/vm/tmpfs-memcg.sh diff --git a/tools/testing/selftests/vm/.gitignore b/tools/testing/selftests/vm/.gitignore index 2e7e86e852828..cb229974c5f15 100644 --- a/tools/testing/selftests/vm/.gitignore +++ b/tools/testing/selftests/vm/.gitignore @@ -19,6 +19,7 @@ madv_populate userfaultfd mlock-intersect-test mlock-random-test +mmap_write virtual_address_range gup_test va_128TBswitch diff --git a/tools/testing/selftests/vm/mmap_write.c b/tools/testing/selftests/vm/mmap_write.c new file mode 100644 index 0000000000000..88a8468f2128c --- /dev/null +++ b/tools/testing/selftests/vm/mmap_write.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This program faults memory in tmpfs + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Global definitions. */ + +/* Global variables. */ +static const char *self; +static char *shmaddr; +static int shmid; + +/* + * Show usage and exit. + */ +static void exit_usage(void) +{ + printf("Usage: %s -p -s \n", self); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) +{ + int fd = 0; + int key = 0; + int *ptr = NULL; + int c = 0; + int size = 0; + char path[256] = ""; + int want_sleep = 0, private = 0; + int populate = 0; + int write = 0; + int reserve = 1; + + /* Parse command-line arguments. */ + setvbuf(stdout, NULL, _IONBF, 0); + self = argv[0]; + + while ((c = getopt(argc, argv, ":s:p:")) != -1) { + switch (c) { + case 's': + size = atoi(optarg); + break; + case 'p': + strncpy(path, optarg, sizeof(path)); + break; + default: + errno = EINVAL; + perror("Invalid arg"); + exit_usage(); + } + } + + printf("%s\n", path); + if (strncmp(path, "", sizeof(path)) != 0) { + printf("Writing to this path: %s\n", path); + } else { + errno = EINVAL; + perror("path not found"); + exit_usage(); + } + + if (size != 0) { + printf("Writing this size: %d\n", size); + } else { + errno = EINVAL; + perror("size not found"); + exit_usage(); + } + + fd = open(path, O_CREAT | O_RDWR, 0777); + if (fd == -1) + err(1, "Failed to open file."); + + if (ftruncate(fd, size)) + err(1, "failed to ftruncate %s", path); + + ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (ptr == MAP_FAILED) { + close(fd); + err(1, "Error mapping the file"); + } + + printf("Writing to memory.\n"); + memset(ptr, 1, size); + printf("Done writing to memory.\n"); + close(fd); + + return 0; +} diff --git a/tools/testing/selftests/vm/tmpfs-memcg.sh b/tools/testing/selftests/vm/tmpfs-memcg.sh new file mode 100755 index 0000000000000..eb584ddcbae5f --- /dev/null +++ b/tools/testing/selftests/vm/tmpfs-memcg.sh @@ -0,0 +1,87 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +CGROUP_PATH=/dev/cgroup/memory/tmpfs-memcg-test + +function cleanup() { + rm -rf /mnt/tmpfs/* + umount /mnt/tmpfs + rm -rf /mnt/tmpfs + + rmdir $CGROUP_PATH + + echo CLEANUP DONE +} + +function setup() { + mkdir -p $CGROUP_PATH + echo $((10 * 1024 * 1024)) > $CGROUP_PATH/memory.limit_in_bytes + echo 0 > $CGROUP_PATH/cpuset.cpus + echo 0 > $CGROUP_PATH/cpuset.mems + + mkdir -p /mnt/tmpfs + + echo SETUP DONE +} + +function expect_equal() { + local expected="$1" + local actual="$2" + local error="$3" + + if [[ "$actual" != "$expected" ]]; then + echo "expected ($expected) != actual ($actual): $3" >&2 + cleanup + exit 1 + fi +} + +function expect_ge() { + local expected="$1" + local actual="$2" + local error="$3" + + if [[ "$actual" -lt "$expected" ]]; then + echo "expected ($expected) < actual ($actual): $3" >&2 + cleanup + exit 1 + fi +} + +cleanup +setup + +mount -t tmpfs -o memcg=$CGROUP_PATH tmpfs /mnt/tmpfs + +TARGET_MEMCG_USAGE=$(cat $CGROUP_PATH/memory.usage_in_bytes) +expect_equal 0 "$TARGET_MEMCG_USAGE" "Before echo, memcg usage should be 0" + +# Echo to allocate a page in the tmpfs +echo hello > /mnt/tmpfs/test +TARGET_MEMCG_USAGE=$(cat $CGROUP_PATH/memory.usage_in_bytes) +expect_ge 4096 "$TARGET_MEMCG_USAGE" "After echo, memcg usage should be greater than 4096" +echo "Echo test succeeded" + +tools/testing/selftests/vm/mmap_write -p /mnt/tmpfs/test -s $((1 * 1024 * 1024)) +TARGET_MEMCG_USAGE=$(cat $CGROUP_PATH/memory.usage_in_bytes) +expect_ge $((1 * 1024 * 1024)) "$TARGET_MEMCG_USAGE" "After echo, memcg usage should greater than 1MB" +echo "Write succeeded" + +# OOM the remote container on pagefault. +echo +echo +echo "OOMing the remote container using pagefault." +echo "This will take a long time because the kernel goes through reclaim retries," +echo "but should eventually be OOM-killed by 'Out of memory (Killing remote allocating task)'" +#tools/testing/selftests/vm/mmap_write -p /mnt/tmpfs/test -s $((11 * 1024 * 1024)) + +# OOM the remote container on non pagefault. +echo +echo +echo "OOMing the remote container using cat (non-pagefault)" +echo "This will take a long time because the kernel goes through reclaim retries," +echo "but should eventually the cat command should receive an ENOMEM" +cat /dev/random > /mnt/tmpfs/random + +cleanup +echo SUCCESS -- 2.34.0.rc0.344.g81b53c2807-goog 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15787C433F5 for ; Wed, 10 Nov 2021 21:20:10 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B2BC8611F2 for ; Wed, 10 Nov 2021 21:20:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B2BC8611F2 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 5A7976B0074; Wed, 10 Nov 2021 16:20:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 530D16B0075; Wed, 10 Nov 2021 16:20:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F8C26B0078; Wed, 10 Nov 2021 16:20:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0038.hostedemail.com [216.40.44.38]) by kanga.kvack.org (Postfix) with ESMTP id 2D14D6B0074 for ; Wed, 10 Nov 2021 16:20:09 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id E19501851200F for ; Wed, 10 Nov 2021 21:20:08 +0000 (UTC) X-FDA: 78794288538.14.F9BCF62 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf24.hostedemail.com (Postfix) with ESMTP id 92682B0000BC for ; Wed, 10 Nov 2021 21:20:08 +0000 (UTC) Received: by mail-pf1-f202.google.com with SMTP id x14-20020a627c0e000000b0049473df362dso2634707pfc.12 for ; Wed, 10 Nov 2021 13:20:08 -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:cc; bh=XWUyVcqFDorfkxK8BmUlQ2jLw5lcWmVuPPsoovex71Y=; b=G2nMJLGxPIMyEeUfb/vaasX85o5Sfnbuzq+2YEd/YK686uuZTNaruqnosc1t/nwbaF m5N05wjs10IyzymZE1gJ6SWhZ1AMTcdNqaVe9/ePQ2lp28a0MtFXaJFaLSD8rvEzo5Wh arhruu4iKXYXKUfbZPe5gNtak87IS9WUPyWzKzMJN8Yxwl5hewD/kaNYM4lIxyeTf+fa nBx6tWsFO5qj9ltm0CS79xcUMAQAurs6zyFOmzCmBBqH4fRRF/TfEkkeKLL+FAEsaSzs RtO8Yf1co/HOZQINwy2CIqTxLQgBoMSO3o/5uxwPDjYidsajo4DRAO7ZTOxHZhxVZTK5 Ornw== 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:cc; bh=XWUyVcqFDorfkxK8BmUlQ2jLw5lcWmVuPPsoovex71Y=; b=axeLiPOxr9FedwvHdIqwMYx3bmVzcTWrAnVz2yv0dNrZkB242Y7PCmBc/j8bEbFZib GvMP8bkqKZT/SVnWtKtXFwCGHolHm780AElsRyMAB+RlKZJz1Uko9+c/kEfFQpq922uf wYV2GFfipA5O8Msqk/o235g4F/cRbdiU86DEJHu2HS4oow3KRpa6q+gYdtAUZ8GCYmYr n+fQ6W2Wc+tD/deeNnP8GubH6aZh6CgYsICNRfnl0Ox4EeR3MNoFihLpnJYvkC0KcGC5 sq8I2h1eNXCBiAii2/P6cMWc1699jCdHoYlH5zfdDHG/oEHrmCYp0g9XmEZVZ+MuIM+7 JaBg== X-Gm-Message-State: AOAM530zNQ/yK+70cn1Dv1wAXfMx/kw6IYemH4piK3N8hlX2KtbxvoBw Kigupa6JxKM2hxVuyTI9kz43Gq1lvCpDYvdwlQ== X-Google-Smtp-Source: ABdhPJwgK2AbRmOmsVL7fZ0uDB5Xq1S64WUY/kQU6kiEqA95TWLlPwwtuaHfqsCtshAjZpFdVr+GfBeRh9uNMzO7/w== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2cd:202:59c8:7b4e:e859:9db0]) (user=almasrymina job=sendgmr) by 2002:a62:4e87:0:b0:47b:dbbf:c6f0 with SMTP id c129-20020a624e87000000b0047bdbbfc6f0mr2005596pfb.47.1636579207325; Wed, 10 Nov 2021 13:20:07 -0800 (PST) Date: Wed, 10 Nov 2021 13:19:50 -0800 In-Reply-To: <20211110211951.3730787-1-almasrymina@google.com> Message-Id: <20211110211951.3730787-5-almasrymina@google.com> Mime-Version: 1.0 References: <20211110211951.3730787-1-almasrymina@google.com> X-Mailer: git-send-email 2.34.0.rc0.344.g81b53c2807-goog Subject: [PATCH v2 4/4] mm, shmem, selftests: add tmpfs memcg= mount option tests From: Mina Almasry Cc: Mina Almasry , Michal Hocko , "Theodore Ts'o" , Greg Thelen , Shakeel Butt , Andrew Morton , Hugh Dickins , Roman Gushchin , Dave Chinner , Johannes Weiner , Tejun Heo , Vladimir Davydov , Muchun Song , riel@surriel.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 92682B0000BC X-Stat-Signature: pud5qd39513fj4kzca1okr68x5pococi Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=G2nMJLGx; spf=pass (imf24.hostedemail.com: domain of 3hzeMYQsKCDoWhiWonuiejWckkcha.Ykihejqt-iigrWYg.knc@flex--almasrymina.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3hzeMYQsKCDoWhiWonuiejWckkcha.Ykihejqt-iigrWYg.knc@flex--almasrymina.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1636579208-985741 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: Signed-off-by: Mina Almasry Cc: Michal Hocko Cc: Theodore Ts'o Cc: Greg Thelen Cc: Shakeel Butt Cc: Andrew Morton Cc: Hugh Dickins Cc: Roman Gushchin Cc: Dave Chinner Cc: Johannes Weiner Cc: Hugh Dickins Cc: Tejun Heo Cc: Vladimir Davydov Cc: Muchun Song Cc: riel@surriel.com Cc: linux-mm@kvack.org Cc: linux-fsdevel@vger.kernel.org Cc: cgroups@vger.kernel.org --- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/mmap_write.c | 103 ++++++++++++++++++++++ tools/testing/selftests/vm/tmpfs-memcg.sh | 87 ++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 tools/testing/selftests/vm/mmap_write.c create mode 100755 tools/testing/selftests/vm/tmpfs-memcg.sh diff --git a/tools/testing/selftests/vm/.gitignore b/tools/testing/selftests/vm/.gitignore index 2e7e86e852828..cb229974c5f15 100644 --- a/tools/testing/selftests/vm/.gitignore +++ b/tools/testing/selftests/vm/.gitignore @@ -19,6 +19,7 @@ madv_populate userfaultfd mlock-intersect-test mlock-random-test +mmap_write virtual_address_range gup_test va_128TBswitch diff --git a/tools/testing/selftests/vm/mmap_write.c b/tools/testing/selftests/vm/mmap_write.c new file mode 100644 index 0000000000000..88a8468f2128c --- /dev/null +++ b/tools/testing/selftests/vm/mmap_write.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This program faults memory in tmpfs + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Global definitions. */ + +/* Global variables. */ +static const char *self; +static char *shmaddr; +static int shmid; + +/* + * Show usage and exit. + */ +static void exit_usage(void) +{ + printf("Usage: %s -p -s \n", self); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) +{ + int fd = 0; + int key = 0; + int *ptr = NULL; + int c = 0; + int size = 0; + char path[256] = ""; + int want_sleep = 0, private = 0; + int populate = 0; + int write = 0; + int reserve = 1; + + /* Parse command-line arguments. */ + setvbuf(stdout, NULL, _IONBF, 0); + self = argv[0]; + + while ((c = getopt(argc, argv, ":s:p:")) != -1) { + switch (c) { + case 's': + size = atoi(optarg); + break; + case 'p': + strncpy(path, optarg, sizeof(path)); + break; + default: + errno = EINVAL; + perror("Invalid arg"); + exit_usage(); + } + } + + printf("%s\n", path); + if (strncmp(path, "", sizeof(path)) != 0) { + printf("Writing to this path: %s\n", path); + } else { + errno = EINVAL; + perror("path not found"); + exit_usage(); + } + + if (size != 0) { + printf("Writing this size: %d\n", size); + } else { + errno = EINVAL; + perror("size not found"); + exit_usage(); + } + + fd = open(path, O_CREAT | O_RDWR, 0777); + if (fd == -1) + err(1, "Failed to open file."); + + if (ftruncate(fd, size)) + err(1, "failed to ftruncate %s", path); + + ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (ptr == MAP_FAILED) { + close(fd); + err(1, "Error mapping the file"); + } + + printf("Writing to memory.\n"); + memset(ptr, 1, size); + printf("Done writing to memory.\n"); + close(fd); + + return 0; +} diff --git a/tools/testing/selftests/vm/tmpfs-memcg.sh b/tools/testing/selftests/vm/tmpfs-memcg.sh new file mode 100755 index 0000000000000..eb584ddcbae5f --- /dev/null +++ b/tools/testing/selftests/vm/tmpfs-memcg.sh @@ -0,0 +1,87 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +CGROUP_PATH=/dev/cgroup/memory/tmpfs-memcg-test + +function cleanup() { + rm -rf /mnt/tmpfs/* + umount /mnt/tmpfs + rm -rf /mnt/tmpfs + + rmdir $CGROUP_PATH + + echo CLEANUP DONE +} + +function setup() { + mkdir -p $CGROUP_PATH + echo $((10 * 1024 * 1024)) > $CGROUP_PATH/memory.limit_in_bytes + echo 0 > $CGROUP_PATH/cpuset.cpus + echo 0 > $CGROUP_PATH/cpuset.mems + + mkdir -p /mnt/tmpfs + + echo SETUP DONE +} + +function expect_equal() { + local expected="$1" + local actual="$2" + local error="$3" + + if [[ "$actual" != "$expected" ]]; then + echo "expected ($expected) != actual ($actual): $3" >&2 + cleanup + exit 1 + fi +} + +function expect_ge() { + local expected="$1" + local actual="$2" + local error="$3" + + if [[ "$actual" -lt "$expected" ]]; then + echo "expected ($expected) < actual ($actual): $3" >&2 + cleanup + exit 1 + fi +} + +cleanup +setup + +mount -t tmpfs -o memcg=$CGROUP_PATH tmpfs /mnt/tmpfs + +TARGET_MEMCG_USAGE=$(cat $CGROUP_PATH/memory.usage_in_bytes) +expect_equal 0 "$TARGET_MEMCG_USAGE" "Before echo, memcg usage should be 0" + +# Echo to allocate a page in the tmpfs +echo hello > /mnt/tmpfs/test +TARGET_MEMCG_USAGE=$(cat $CGROUP_PATH/memory.usage_in_bytes) +expect_ge 4096 "$TARGET_MEMCG_USAGE" "After echo, memcg usage should be greater than 4096" +echo "Echo test succeeded" + +tools/testing/selftests/vm/mmap_write -p /mnt/tmpfs/test -s $((1 * 1024 * 1024)) +TARGET_MEMCG_USAGE=$(cat $CGROUP_PATH/memory.usage_in_bytes) +expect_ge $((1 * 1024 * 1024)) "$TARGET_MEMCG_USAGE" "After echo, memcg usage should greater than 1MB" +echo "Write succeeded" + +# OOM the remote container on pagefault. +echo +echo +echo "OOMing the remote container using pagefault." +echo "This will take a long time because the kernel goes through reclaim retries," +echo "but should eventually be OOM-killed by 'Out of memory (Killing remote allocating task)'" +#tools/testing/selftests/vm/mmap_write -p /mnt/tmpfs/test -s $((11 * 1024 * 1024)) + +# OOM the remote container on non pagefault. +echo +echo +echo "OOMing the remote container using cat (non-pagefault)" +echo "This will take a long time because the kernel goes through reclaim retries," +echo "but should eventually the cat command should receive an ENOMEM" +cat /dev/random > /mnt/tmpfs/random + +cleanup +echo SUCCESS -- 2.34.0.rc0.344.g81b53c2807-goog From mboxrd@z Thu Jan 1 00:00:00 1970 From: Mina Almasry Subject: [PATCH v2 4/4] mm, shmem, selftests: add tmpfs memcg= mount option tests Date: Wed, 10 Nov 2021 13:19:50 -0800 Message-ID: <20211110211951.3730787-5-almasrymina@google.com> References: <20211110211951.3730787-1-almasrymina@google.com> Mime-Version: 1.0 Return-path: 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:cc; bh=XWUyVcqFDorfkxK8BmUlQ2jLw5lcWmVuPPsoovex71Y=; b=G2nMJLGxPIMyEeUfb/vaasX85o5Sfnbuzq+2YEd/YK686uuZTNaruqnosc1t/nwbaF m5N05wjs10IyzymZE1gJ6SWhZ1AMTcdNqaVe9/ePQ2lp28a0MtFXaJFaLSD8rvEzo5Wh arhruu4iKXYXKUfbZPe5gNtak87IS9WUPyWzKzMJN8Yxwl5hewD/kaNYM4lIxyeTf+fa nBx6tWsFO5qj9ltm0CS79xcUMAQAurs6zyFOmzCmBBqH4fRRF/TfEkkeKLL+FAEsaSzs RtO8Yf1co/HOZQINwy2CIqTxLQgBoMSO3o/5uxwPDjYidsajo4DRAO7ZTOxHZhxVZTK5 Ornw== In-Reply-To: <20211110211951.3730787-1-almasrymina-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org> List-ID: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Cc: Mina Almasry , Michal Hocko , Theodore Ts'o , Greg Thelen , Shakeel Butt , Andrew Morton , Hugh Dickins , Roman Gushchin , Dave Chinner , Johannes Weiner , Tejun Heo , Vladimir Davydov , Muchun Song , riel-ebMLmSuQjDVBDgjK7y7TUQ@public.gmane.org, linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org, linux-fsdevel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org Signed-off-by: Mina Almasry Cc: Michal Hocko Cc: Theodore Ts'o Cc: Greg Thelen Cc: Shakeel Butt Cc: Andrew Morton Cc: Hugh Dickins Cc: Roman Gushchin Cc: Dave Chinner Cc: Johannes Weiner Cc: Hugh Dickins Cc: Tejun Heo Cc: Vladimir Davydov Cc: Muchun Song Cc: riel-ebMLmSuQjDVBDgjK7y7TUQ@public.gmane.org Cc: linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org Cc: linux-fsdevel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org Cc: cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org --- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/mmap_write.c | 103 ++++++++++++++++++++++ tools/testing/selftests/vm/tmpfs-memcg.sh | 87 ++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 tools/testing/selftests/vm/mmap_write.c create mode 100755 tools/testing/selftests/vm/tmpfs-memcg.sh diff --git a/tools/testing/selftests/vm/.gitignore b/tools/testing/selftests/vm/.gitignore index 2e7e86e852828..cb229974c5f15 100644 --- a/tools/testing/selftests/vm/.gitignore +++ b/tools/testing/selftests/vm/.gitignore @@ -19,6 +19,7 @@ madv_populate userfaultfd mlock-intersect-test mlock-random-test +mmap_write virtual_address_range gup_test va_128TBswitch diff --git a/tools/testing/selftests/vm/mmap_write.c b/tools/testing/selftests/vm/mmap_write.c new file mode 100644 index 0000000000000..88a8468f2128c --- /dev/null +++ b/tools/testing/selftests/vm/mmap_write.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This program faults memory in tmpfs + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Global definitions. */ + +/* Global variables. */ +static const char *self; +static char *shmaddr; +static int shmid; + +/* + * Show usage and exit. + */ +static void exit_usage(void) +{ + printf("Usage: %s -p -s \n", self); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) +{ + int fd = 0; + int key = 0; + int *ptr = NULL; + int c = 0; + int size = 0; + char path[256] = ""; + int want_sleep = 0, private = 0; + int populate = 0; + int write = 0; + int reserve = 1; + + /* Parse command-line arguments. */ + setvbuf(stdout, NULL, _IONBF, 0); + self = argv[0]; + + while ((c = getopt(argc, argv, ":s:p:")) != -1) { + switch (c) { + case 's': + size = atoi(optarg); + break; + case 'p': + strncpy(path, optarg, sizeof(path)); + break; + default: + errno = EINVAL; + perror("Invalid arg"); + exit_usage(); + } + } + + printf("%s\n", path); + if (strncmp(path, "", sizeof(path)) != 0) { + printf("Writing to this path: %s\n", path); + } else { + errno = EINVAL; + perror("path not found"); + exit_usage(); + } + + if (size != 0) { + printf("Writing this size: %d\n", size); + } else { + errno = EINVAL; + perror("size not found"); + exit_usage(); + } + + fd = open(path, O_CREAT | O_RDWR, 0777); + if (fd == -1) + err(1, "Failed to open file."); + + if (ftruncate(fd, size)) + err(1, "failed to ftruncate %s", path); + + ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (ptr == MAP_FAILED) { + close(fd); + err(1, "Error mapping the file"); + } + + printf("Writing to memory.\n"); + memset(ptr, 1, size); + printf("Done writing to memory.\n"); + close(fd); + + return 0; +} diff --git a/tools/testing/selftests/vm/tmpfs-memcg.sh b/tools/testing/selftests/vm/tmpfs-memcg.sh new file mode 100755 index 0000000000000..eb584ddcbae5f --- /dev/null +++ b/tools/testing/selftests/vm/tmpfs-memcg.sh @@ -0,0 +1,87 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +CGROUP_PATH=/dev/cgroup/memory/tmpfs-memcg-test + +function cleanup() { + rm -rf /mnt/tmpfs/* + umount /mnt/tmpfs + rm -rf /mnt/tmpfs + + rmdir $CGROUP_PATH + + echo CLEANUP DONE +} + +function setup() { + mkdir -p $CGROUP_PATH + echo $((10 * 1024 * 1024)) > $CGROUP_PATH/memory.limit_in_bytes + echo 0 > $CGROUP_PATH/cpuset.cpus + echo 0 > $CGROUP_PATH/cpuset.mems + + mkdir -p /mnt/tmpfs + + echo SETUP DONE +} + +function expect_equal() { + local expected="$1" + local actual="$2" + local error="$3" + + if [[ "$actual" != "$expected" ]]; then + echo "expected ($expected) != actual ($actual): $3" >&2 + cleanup + exit 1 + fi +} + +function expect_ge() { + local expected="$1" + local actual="$2" + local error="$3" + + if [[ "$actual" -lt "$expected" ]]; then + echo "expected ($expected) < actual ($actual): $3" >&2 + cleanup + exit 1 + fi +} + +cleanup +setup + +mount -t tmpfs -o memcg=$CGROUP_PATH tmpfs /mnt/tmpfs + +TARGET_MEMCG_USAGE=$(cat $CGROUP_PATH/memory.usage_in_bytes) +expect_equal 0 "$TARGET_MEMCG_USAGE" "Before echo, memcg usage should be 0" + +# Echo to allocate a page in the tmpfs +echo hello > /mnt/tmpfs/test +TARGET_MEMCG_USAGE=$(cat $CGROUP_PATH/memory.usage_in_bytes) +expect_ge 4096 "$TARGET_MEMCG_USAGE" "After echo, memcg usage should be greater than 4096" +echo "Echo test succeeded" + +tools/testing/selftests/vm/mmap_write -p /mnt/tmpfs/test -s $((1 * 1024 * 1024)) +TARGET_MEMCG_USAGE=$(cat $CGROUP_PATH/memory.usage_in_bytes) +expect_ge $((1 * 1024 * 1024)) "$TARGET_MEMCG_USAGE" "After echo, memcg usage should greater than 1MB" +echo "Write succeeded" + +# OOM the remote container on pagefault. +echo +echo +echo "OOMing the remote container using pagefault." +echo "This will take a long time because the kernel goes through reclaim retries," +echo "but should eventually be OOM-killed by 'Out of memory (Killing remote allocating task)'" +#tools/testing/selftests/vm/mmap_write -p /mnt/tmpfs/test -s $((11 * 1024 * 1024)) + +# OOM the remote container on non pagefault. +echo +echo +echo "OOMing the remote container using cat (non-pagefault)" +echo "This will take a long time because the kernel goes through reclaim retries," +echo "but should eventually the cat command should receive an ENOMEM" +cat /dev/random > /mnt/tmpfs/random + +cleanup +echo SUCCESS