From patchwork Mon Nov 8 21:19:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 12609115 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 41307C433F5 for ; Mon, 8 Nov 2021 21:20:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E6D2261052 for ; Mon, 8 Nov 2021 21:20:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E6D2261052 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 927256B0075; Mon, 8 Nov 2021 16:20:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 88C516B0078; Mon, 8 Nov 2021 16:20:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7553E6B007B; Mon, 8 Nov 2021 16:20:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0143.hostedemail.com [216.40.44.143]) by kanga.kvack.org (Postfix) with ESMTP id 5721B6B0075 for ; Mon, 8 Nov 2021 16:20:18 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 14F1618432CAC for ; Mon, 8 Nov 2021 21:20:18 +0000 (UTC) X-FDA: 78787031316.22.DA2167E Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf13.hostedemail.com (Postfix) with ESMTP id 5F233104F79E for ; Mon, 8 Nov 2021 21:20:07 +0000 (UTC) Received: by mail-pl1-f201.google.com with SMTP id e10-20020a17090301ca00b00141fbe2569dso7138739plh.14 for ; Mon, 08 Nov 2021 13:20:17 -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=LgzEEqLYmw5ziRk+Zpmuo/4f8TX/yoKSNyIDF3U5Tf0=; b=dkS19bQQYgTX6/T8jpDaR8pAVWMYF9eTHs7ItoEw3LmZ6NEboTY5qgaJunrQFd4nxd hmIH43JH05TYBkiNZHmt7mz6SQpIc2T1RebaDAq+1gqUPbjJJwR8AV3q+lwvAj2efYGP Ec7gzZ2pGVc+bS/HMhHLxxkVEiilmFOfyUWuktV2i91mDTDQ+rvUrdoh03H2cpyPTqYA qLcppI1NNSqtON/IG5KuPPu/mL47x7Vh8EGcgUbF902dOTymSW8R3F8xglbi2XWS1Y8N ImS/vJb3SmqXzR0z2tm19uedWyIS8L2xDAAIxDIbtsYANfcHr9OtX5Ab8TBfp7zN+qrN GQTg== 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=LgzEEqLYmw5ziRk+Zpmuo/4f8TX/yoKSNyIDF3U5Tf0=; b=PawpY6pIwsUBEmtbogcJ4tDvSyivKpfWm8vvCA009KXoVygZuRap0CvZ6GVPierSyK Ai3kL7bpyWHCKQQmFBsNjBsxzAVzVT6DyfnuNCEZTRbkke6veSEoJs6rmI6DP2IQVrno Jh5Fu9OE87zLlsS7oDr13hRhWj2p2OsLo0JNaXEWAXl8xRa19OGyq4XE893eKFwkqZEr B0JdASBQ0puAUTP7NMPd74OrK6eMRoh7O1h1aUGiU3SjDPDrrtVlTXjS/iEFtQv4Gdsx M+XIMPZyPdq/Peun2/8nQrwt+lVD2zFVSPROHTUKe2mCJt3qjE1eNYNNMRH/CbT78Pzm eYvQ== X-Gm-Message-State: AOAM5334llNzDGApz5iKOeGIEAZV8jhqu/6nqS2++CH3LbdUfrXkm3eE WzD6Iho5jQALs7reSAUqB0oUKLo00YYfZcfuzg== X-Google-Smtp-Source: ABdhPJz/4wwyS8huhn9kLJ1CmDJcWry/ijIFPMY/kO2XaGL2/Kt4rjB472R+qFOc6hisw6Uhe1o3ER09e9RLY12zdw== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2cd:202:8717:7707:fb59:664e]) (user=almasrymina job=sendgmr) by 2002:a17:902:654d:b0:141:7df3:b94 with SMTP id d13-20020a170902654d00b001417df30b94mr2306024pln.60.1636406416836; Mon, 08 Nov 2021 13:20:16 -0800 (PST) Date: Mon, 8 Nov 2021 13:19:59 -0800 In-Reply-To: <20211108211959.1750915-1-almasrymina@google.com> Message-Id: <20211108211959.1750915-6-almasrymina@google.com> Mime-Version: 1.0 References: <20211108211959.1750915-1-almasrymina@google.com> X-Mailer: git-send-email 2.34.0.rc0.344.g81b53c2807-goog Subject: [PATCH v1 5/5] 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 , Johannes Weiner , Tejun Heo , Vladimir Davydov , riel@surriel.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 5F233104F79E X-Stat-Signature: zqsp1hdqzxjhfcko6du8io79fb6s9rf6 Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=dkS19bQQ; spf=pass (imf13.hostedemail.com: domain of 3kJSJYQsKCPETefTlkrfbgTZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--almasrymina.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3kJSJYQsKCPETefTlkrfbgTZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--almasrymina.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1636406407-468365 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: 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 | 105 ++++++++++++++++++++++ tools/testing/selftests/vm/tmpfs-memcg.sh | 70 +++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 tools/testing/selftests/vm/mmap_write.c create mode 100755 tools/testing/selftests/vm/tmpfs-memcg.sh -- 2.34.0.rc0.344.g81b53c2807-goog 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..3afeaccea9f44 --- /dev/null +++ b/tools/testing/selftests/vm/mmap_write.c @@ -0,0 +1,105 @@ +// 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(); + } + + printf("Not writing to memory.\n"); + printf("Allocating using HUGETLBFS.\n"); + 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..fe7ffe769f903 --- /dev/null +++ b/tools/testing/selftests/vm/tmpfs-memcg.sh @@ -0,0 +1,70 @@ +#!/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 [[ "$expected" != "$actual" ]]; 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_equal 131072 "$TARGET_MEMCG_USAGE" "After echo, memcg usage should be 131072" +echo "Echo test 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